ForeignKey未在Tastypi中显示

2024-10-02 14:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的模型中定义为模型.ForeignKey不在Tastype中展示。除客户机字段外,将显示所有字段。 快速而棘手的解决方法是在语句模型中添加另一个字段,如

ClientID = models.IntegerField(db_column='Client_id', max_length=32)

但我觉得这不对。有人知道更好的解决办法吗?在

在模型.py在

^{pr2}$

在api.py文件在

class StatementResource(ModelResource):
    class Meta:
        queryset = Statement.objects.all()
        resource_name = 'client'
        allowed_methods = ['get']
        include_resource_uri = False

class ClientResource(ModelResource):
    statements = fields.ToManyField(StatementResource, 'statements', null=True, blank=True, full=True)
    class Meta:
        queryset = Client.objects.all()
        resource_name = 'client'
        allowed_methods = ['get']
        include_resource_uri = False

Tags: namepy模型clienttrueobjectsallresource
1条回答
网友
1楼 · 发布于 2024-10-02 14:23:20

在tastype中,您必须在ModelResources定义中定义所有关系字段。所以,如果在模型中有一些外键,你必须在模型资源中再次定义外键。原因是Tastype必须知道如何处理。必须知道它的序列化、授权、身份验证等。其他方式会忽略该字段。你必须传递相关资源,就这样。在

class StatementResource(ModelResource):
    Client = fields.ForeignKey(ClientResource, 'Client')
    class Meta:
        queryset = Statement.objects.all()
        resource_name = 'client'
        allowed_methods = ['get']
        include_resource_uri = False

相关问题 更多 >