我正在尝试从另一个应用程序的模型中获取当前应用程序中的外键。这就是model.py
的样子。
class DataQuestion(models.Model):
...
PollF = models.ForeignKey('myapp2.PollF')
如果我尝试在tasypie中使用它,我会得到null
值。这就是我api.py
的样子。
from myapp2.models import DataQuestion
class HelperSurveyResource(ModelResource):
class Meta:
queryset = DataQuestion.objects.all()
resource_name='some_name'
...
def dehydrate(self,bundle):
bundle.data['complete']=True if(bundle.obj.isPComplete) else False
return bundle
class QnResource(ModelResource):
pollF=fields.ForeignKey(HelperSurveyResource,'some_name',full=True,null=True)
class Meta:
queryset=DataQuestion.objects.all()
这是我得到的结果:
{"pollF": null,
"numAnswers": 1,
....
"text": "How much do you earn per day?"},
如何获取非空值?有帮助吗?感谢
--------- --------- EDIT
问题在IRC上得到了解决。非常感谢Funkybob
错误是'some_name'应该是pollF
。
答案 0 :(得分:0)
对于Jure C.这是答案:
from myapp2.models import DataQuestion
class HelperSurveyResource(ModelResource):
class Meta:
queryset = DataQuestion.objects.all()
resource_name='some_name'
...
def dehydrate(self,bundle):
bundle.data['complete']=True if(bundle.obj.isPComplete) else False
return bundle
此处发生更改 - > to PollF它应该是要搜索的属性的名称。我不知道这个
class QnResource(ModelResource):
pollF=fields.ForeignKey(HelperSurveyResource,'pollF',full=True,null=True)
class Meta:
queryset=DataQuestion.objects.all()