假设我有3个模型:
class MeterialPricing(models.Model):
list_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
book_pricing = models.ForeignKey('books.BookPricing', null=True, blank=True)
inventory_pricing = models.ForeignKey('store.InventoryPricing', null=True, blank=True)
class BookPricing(models.Model):
list_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
class InventoryPricing(models.Model):
list_price = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
和序列化程序类:
class MaterialPricingSerializer(ModelSerializer):
class Meta:
model = MaterialPricingShell
fields = ('pk', 'list_price',)
如您所见,现在,序列化的列表价格来自MaterialPricing模型
如果存在BookPricing或InventoryPricinglist\u price,我想将序列化的list\u price设置为其中之一。BookPricing中的标价优先级最高,然后是InventoryPricing,如果两者都为空,那么我们可以让标价成为MeterialPricing模型中的标价
我正在研究使用SerializerMethodField(),但我不确定是否有某种post钩子来查看BookPricing或InventoryPricing模型上是否存在标价
目前没有回答
相关问题 更多 >
编程相关推荐