So in order to create an object model, I have to first see if a supplier has a relation of type many to many from BusinessModel, WarehouseModel or StoreModel to in a django-polymorphic
My main idea is to check if BusinessModel, which is connected to Order through BusinessOrderModel, StoreModel, which is connected to Order through StoreOrderModel or WarehouseModel which is connected directly to Order, have FKs on a many to many relationship going to the article that we want to order through a POST request.
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([email protected]):
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)
Here's my serializers:
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
}
and here's my view:
class OrderCreateView(generics.CreateAPIView):
queryset = OrderModel.objects.all()
serializer_class = OrderCreateSerializer
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…