如何根据我的规则定义自定义检查以及如何实现Django

kzipqqlq  于 2022-12-05  发布在  Go
关注(0)|答案(1)|浏览(166)

我使用Python 3.10,Django 4.1.2,Djangorestframework ==3.14.0(前面分开)
在订单中,接收到的产品字段默认为空。当我们收到订单时,我们必须从订单字段中删除这些元素,并将它们转移到接收到的字段中。
1.收到的产品必须仅包含来自请求产品的产品
1.提交包含已接收产品数量的请求后,应从请求的产品中删除此特定产品,并将其添加到recived_products中
我有两个理论实现的想法。
1.使用修补程序、received_product和其中的元素
1.分离方法
我有这样的代码:

class Orders(models.Model):
    delivery_model_choices = (("Pickup", "Pickup"),
                              ("Delivery", "Delivery"))
    order_status_choices = (("Draft", "Draft"),
                            ("Open", "Open"),
                            ("Partially Received", "Partially Received"),
                            ("Received", "Received"),
                            ("Cancelled", "Cancelled"))
    costumer = models.ManyToManyField(Costumers)
    products = models.ManyToManyField(Products)
    recived_products = ???
    date_create = models.DateTimeField(auto_now_add=True)
    delivery = models.CharField(max_length=40, choices=delivery_model_choices)
    delivery_date = models.DateField()
    order_status = models.CharField(max_length=40, choices=order_status_choices)
    total_price = models.CharField(max_length=10)

请提供一个关于此实现的正确示例。

wbgh16ku

wbgh16ku1#

我不会写完整的代码,但你可以试试这个逻辑-
为视图集或视图(无论使用什么)定义Create方法

def create(self, request, format=None):
    request.data is the data that you receive
    all_product_recieved = all products that you have received
    recived_products = all_product_recieved - ordered product
    custom_data = create a new dictionary with valid data
    then ...
    serializer = self.get_serializer(data=custom_data)
    if serializer.is_valid():
        serializer.save()
        return Response()
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

希望这对你有帮助。

相关问题