2013-02-16 37 views
0

你好,我有以下問題(對不起我的英文不好)蟒蛇的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)) 

回答

0

您idjuego是一個外鍵,以便該值必須等同於ID,

嘗試:

temporal = Prediccion(idusuario = user, idjuego = game.id, equipoa=int(resa), equipob=int(resb)) 

此外,在各你的模型,請把unicode,以便它不會返回「<對象>」。下面是一個示例:

def __unicode__(self): 
    return self.field_name 
+1

無法分配「16L」:「Prediccion.idjuego」必須是「Juego」實例。 – 2013-02-17 00:23:24

0
temporal.idjuego_id = game.id 
temporal.save() 

ForeignKey的領域存儲在年底,您可以直接訪問,避免訪問數據庫_id屬性的價值。

ForeignKey的_id版本是Django的一個特別有用的方面,每個人都應該在適當的時候隨時瞭解和使用。

相關問題