0
models.pyDjango的REST框架序列化相關領域
class Category(models.Model):
name = models.CharField(max_length=128)
class Product(models.Model):
category = models.ManyToManyField(Category, related_name="category")
name = models.CharField(max_length=128)
class ProductVariation(models.Model):
product = models.ForeignKey(Product, related_name="product")
name = models.CharField(max_length=128)
serializers.py
class ProductVariantSerializer(serializers.HyperlinkedModelSerializer)
class Meta:
model = ProductVariation
fields = (
"name",
)
class CategoryDetailSerializer(serializers.Modelserializer):
product_variant = PromotionVariantSerializer(many=True)
class Meta:
model = Category
fields =(
"name",
"product_variant" #how can i do this
)
這裏我要列出所有屬於該類別的產品變型。
我能做些這樣或那樣我想寫的方法來獲取產品變形細節
謝謝你...我怎樣才能優化數據庫打在這裏?? – Thameem
我怎麼能只顯示變體而不是產品。 – Thameem
然後,您可能需要使用序列化方法字段或此類方法,因爲'Category'和'ProductVariation'只通過'Product'模型相關。 – zaidfazil