【发布时间】:2014-08-08 14:38:34
【问题描述】:
在我的模型中定义为 models.ForeignKey 的字段不会显示在 Tastypie 中。显示除客户字段外的所有字段。 快速而肮脏的解决方法是在 Statement 模型中添加另一个字段,例如
ClientID = models.IntegerField(db_column='Client_id', max_length=32)
但这对我来说似乎是错误的。有谁知道更好的解决方案?
models.py
class Client(models.Model):
ID = models.AutoField(primary_key=True)
F1 = models.CharField(max_length=256, null=True)
F2 = models.CharField(max_length=256, null=True)
class Statement(models.Model):
ID = models.AutoField(primary_key=True)
Client = models.ForeignKey(Client, related_name='statements')
State = models.CharField(max_length=256, null=True)
Address = models.CharField(max_length=256, null=True)
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
【问题讨论】: