我的模特就像这样
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
。
答案 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
的原因吗?