【发布时间】:2014-09-30 15:33:46
【问题描述】:
这是我的 models.py 的一部分:
class Discount(models.Model):
discount_id = models.AutoField(primary_key=True)
discount_category = models.ManyToManyField(Category)
discount_store = models.ManyToManyField(Store)
...
class Store(models.Model):
store_id = models.AutoField(primary_key=True)
store_company =models.ForeignKey('Company')
store_city = models.ForeignKey('City', to_field='city_name')
....
这是我的serializer.py的一部分:,
class StoreSerializer(serializers.ModelSerializer):
class Meta:
model = Store
fields = ('store_location', 'store_city', 'store_name', 'store_address')
class DiscountSerializer(serializers.ModelSerializer):
class Meta:
model = Discount
fields = ('discount_description', 'discount_start', 'discount_end', discount_title', 'discount_category', 'discount_store')
当我为折扣创建 JSON 时,我得到了这个 JSON。
[{
"discount_description": "Description here.",
"discount_start": "2014-08-07T14:35:13Z",
"discount_end": "2014-08-30T14:35:15Z",
"discount_title": "Lorem İpsum",
"discount_category": [
3
],
"discount_store": [
1
]
}]
但我想在“discount_store”中查看商店详情。我为此尝试了很多关系。但我总是得到这个 JSON。
【问题讨论】:
-
在我如上重命名表定义中的外键列名后,它开始回升。我认为最好覆盖默认的外键列名并给出类似 store_city 的内容(在这种情况下,迁移后它将是数据库中的 store_city_id),可以在序列化程序类中提及。
标签: python json django serialization django-rest-framework