0

因此,为了创建对象模型,我必须首先查看供应商是否具有从 BusinessModel、WarehouseModel 或 StoreModel 到 django 多态的多对多类型关系

我的主要想法是检查通过 BusinessOrderModel 连接到 Order 的 BusinessModel、通过 StoreOrderModel 连接到 Order 的 StoreModel 或直接连接到 Order 的 WarehouseModel 是否具有指向我们想要的文章的多对多关系的 FK通过 POST 请求订购。

class ArticleModel(models.Model):
    id = models.AutoField(primary_key=True)
    code = models.IntegerField(unique=True)
    description = models.TextField()

    def __str__(self):
        return str(self.code)

class OrderModel(models.Model):
    id = models.AutoField(primary_key=True)
    order_to = models.ForeignKey('OrderToModel', on_delete=models.CASCADE)
    article = models.ForeignKey('ArticleModel', on_delete=models.CASCADE)
    quantity= models.IntegerField()

class OrderToModel(Porrodriguez@odoogrp.comlymorphicModel):
    id = models.AutoField(primary_key=True)

class WarehouseModel(OrderToModel):
    warehouse_num = models.IntegerField(unique=True)
    name = models.CharField(max_length=100)
    address = models.TextField()
    articles = models.ManyToManyField(ArticleModel)
    def __str__(self):
        return "Warehouse"+ str(self.warehouse_num)

class StoreOrderModel(OrderToModel):
    reference = models.IntegerField(unique=True)
    store_num = models.ForeignKey('StoreModel', on_delete=models.CASCADE)
    def __str__(self):
        return str(self.reference)

class StoreModel(models.Model):
    store_num = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    address = models.TextField()
    articles = models.ManyToManyField(ArticleModel)
    def __str__(self):
        return str(self.store_num)

class BusinessOrderModel(OrderToModel):
    reference = models.IntegerField(unique=True)
    business_num = models.ForeignKey('BusinessModel', on_delete=models.CASCADE)
    def __str__(self):
        return str(self.reference)
    
class BusinessModel(models.Model):
    Business_num = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    address = models.TextField()
    articles = models.ManyToManyField(ArticleModel)
    def __str__(self):
        return str(self.Business_num)

这是我的序列化程序:

class WarehouseSerializer(serializers.ModelSerializer):
    class Meta:
        model = WarehouseModel
        fields = ('id', 'warehouse_num')


class StoreOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = StoreOrderModel
        fields = ('id', 'reference', 'store_num')


class DetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = DetailModel
        fields = ('id', 'detail')

class BusinessOrderSerializer(serializers.ModelSerializer):
    details = DetailSerializer(many=True, read_only=True)
    class Meta:
        model = BusinessOrderModel
        fields = ('reference', 'business_num','details')

class OrderToPolymorphicSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        WarehouseModel: WarehouseSerializer,
        StoreOrderModel: StoreOrderSerializer,
        BusinessOrderModel: BusinessOrderSerializer
    }

这是我的观点:

class OrderCreateView(generics.CreateAPIView):
    queryset = OrderModel.objects.all()
    serializer_class = OrderCreateSerializer
4

0 回答 0