Django加入不工作

时间:2014-06-21 16:36:12

标签: python django

我的模特就像这样

class NounPlural(models.Model):
    idnoun_plural = models.IntegerField(db_column='idNoun_Plural', primary_key=True) # Field name made lowercase.
    nns = models.TextField(db_column='NNS', blank=True) # Field name made lowercase.
    news_idnews = models.ForeignKey(News, db_column='news_idnews')

class News(models.Model):
   idnews = models.IntegerField(primary_key=True)
   source = models.TextField(blank=True)
   title = models.TextField(blank=True)

我的view.py就像这样

def allobj(request):
    obj_json = serializers.serialize('json', NounPlural.objects.select_related('news_idnews')[:5] )
    obj_list = json.loads( obj_json )
    json_data = json.dumps( obj_list )
    return HttpResponse( json_data, content_type='application/json' )

我想做的就是获取NounPlural以及来自News的标题。当我转到allobj链接时,我只会获得NounPlural

1 个答案:

答案 0 :(得分:-1)

django documentation related to your question中,我始终认为需要.get(id=<something>)select_related()电话,如下所示:

Book.objects.select_related('person__city').get(id=4)

所以不应该打电话给你:

NounPlural.objects.select_related('news_idnews').get(id=<Newsid>)[:5]

这是您只有NounPlural而不是QuerySet的原因吗?