这是 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。
答案 0 :(得分:2)
您是否尝试将选项depth
添加到Meta类?也许是这样的:
class DiscountSerializer(serializers.ModelSerializer):
class Meta:
model = Discount
fields = ('discount_description', 'discount_start', 'discount_end', 'discount_title', 'discount_category', 'discount_store')
depth = 1