你好,我有以下問題(對不起我的英文不好)蟒蛇的Django無法分配「<JUEGO:JUEGO對象>」:「Prediccion.idjuego」必須是一個「JUEGO」實例
我有以下型號
我有3種型號,其「預測」,有兩個外鍵從「JUEGO」模式和「Usuario」模式
class Juego(models.Model):
#id = models.IntegerField(primary_key=True, db_column='Id')
equipoa = models.CharField(max_length=135, db_column='EquipoA')
equipob = models.CharField(max_length=135, db_column='EquipoB')
resultadoa = models.IntegerField(null=True, db_column='ResultadoA', blank=True)
resultadob = models.IntegerField(null=True, db_column='ResultadoB', blank=True)
fecha = models.DateField(null=True, db_column='Fecha', blank=True)
class Meta:
db_table = u'juego'
class Usuario(models.Model):
# id = models.IntegerField(primary_key=True, db_column='Id') # Field name made lowercase.
nombre = models.CharField(max_length=135, db_column='Nombre')
fechanacimiento = models.DateField(null=True, db_column='FechaNacimiento', blank=True)
nombreusuario = models.CharField(max_length=135, db_column='NombreUsuario')
clave = models.CharField(max_length=135, db_column='Clave')
class Meta:
db_table = u'usuario'
class Prediccion(models.Model):
#id = models.IntegerField(primary_key=True, db_column='Id')
idusuario = models.ForeignKey(AuthUser, db_column='IdUsuario')
idjuego = models.ForeignKey(Juego, db_column='IdJuego') # Field name made lowercase.
equipoa = models.IntegerField(null=True, db_column='EquipoA', blank=True)
equipob = models.IntegerField(null=True, db_column='EquipoB', blank=True)
resultado = models.IntegerField(null=True, db_column='Resultado', blank=True)
class Meta:
db_table = u'prediccion'
而且有我有以下幾點看法
from django.shortcuts import render_to_response
from scorecenter.JuegoApp.models import Juego
from scorecenter.PrediccionApp.models import Prediccion
from scorecenter.PrediccionApp.models import TipoResultado
from scorecenter.PrediccionApp.models import AuthUser
def juegosap(request, pagina="1", idgame=-1, resa=-1, resb=-1):
if(idgame==-1 and resa==-1 and resb==-1):
pag = int(pagina)
pag = pag-1
lista = Juego.objects.order_by('-fecha', '-id')[pag*4:pag*4+4]
template_name = 'juegos_semana.html'
return render_to_response(template_name,{'lista':lista})
else:
game = Juego.objects.get(id=int(idgame))
print(game.equipoa)
print(game.id)
user = AuthUser.objects.get(username=request.user)
print(user.username)
temporal = Prediccion(idusuario = user, idjuego = game, equipoa=int(resa), equipob=int(resb))
temporal.resultado = 1
temporal.save()
pag = int(pagina)
pag = pag-1
lista = Juego.objects.order_by('-fecha')[pag*4:pag*4+4]
template_name = 'juegos_semana.html'
return render_to_response(template_name,{'lista':lista})
但我收到以下錯誤:
Cannot assign "<Juego: Juego object>": "Prediccion.idjuego" must be a "Juego" instance.
in the next line:
temporal = Prediccion(idusuario = user, idjuego = game, equipoa=int(resa), equipob=int(resb))
無法分配「16L」:「Prediccion.idjuego」必須是「Juego」實例。 – 2013-02-17 00:23:24