python - django-tastypie : Related data not saving

标签 python django api rest tastypie

我的models.py

class Orders(models.Model):
    order_id = models.CharField(max_length=7, primary_key=True)
    users = models.ForeignKey(ProductUsers, on_delete=models.DO_NOTHING)
    address = models.ForeignKey(ProductUsersAddress, on_delete=models.DO_NOTHING)
    payment_method = models.CharField(default='COD', max_length=20, choices=PAYMENT_METHOD)

class OrderedProduct(models.Model):
    products = models.ForeignKey(Products, on_delete=models.DO_NOTHING)
    orders = models.ForeignKey(Orders, on_delete=models.CASCADE)
    quantity = models.IntegerField(default=0)
    price = models.DecimalField(default=0.00, max_digits=5, decimal_places=2, blank=False)

我的resources.py

class OrdersResource(ModelResource):

    ordered_products = fields.ToManyField('orders.resources.OrderedProductResource',
                                          attribute=lambda bundle: OrderedProduct.objects.filter(orders=bundle.obj),
                                          related_name='orders', full=True, null=True)

    contact_no = fields.ForeignKey(ProductUsersResource, 'users')
    address = fields.ForeignKey(ProductUsersAddressResource, 'address')

    class Meta:
        queryset = Orders.objects.all()
        resource_name = 'orders'
        include_resource_uri = False
        collection_name = 'orders'
        allowed_methods = ['get', 'post']
        always_return_data = True

class OrderedProductResource(ModelResource):
    products = fields.ForeignKey(ProductsResource, 'products')
    orders = fields.ForeignKey(OrdersResource, 'orders')

    class Meta:
        queryset = OrderedProduct.objects.all()
        resource_name = 'ordered_products'
        excludes = ['id']
        include_resource_uri = False

我使用 Django-Admin 输入数据。 当我点击 http://localhost:8000/orders/ 时,我明白了,

{
  "orders": [
    {
      "address": "/api/v1/address/1",
      "contact_no": "/api/v1/users/8269661606",
      "order_id": "KJLSWI",
      "ordered_products": [
        {
          "orders": "/api/v1/orders/KJLSWI",
          "price": "40.00",
          "products": "/api/v1/products/1",
          "quantity": 2
        },
        {
          "orders": "/api/v1/orders/KJLSWI",
          "price": "70.00",
          "products": "/api/v1/products/2",
          "quantity": 4
        },
        {
          "orders": "/api/v1/orders/KJLSWI",
          "price": "67.00",
          "products": "/api/v1/products/3",
          "quantity": 7
        }
      ],
      "payment_method": "COD",
    }
  ]
}

现在根据 tasty 文档,

Tastypie encourages “round-trippable” data, which means the data you can GET should be able to be POST/PUT’d back to recreate the same object. If you’re ever in question about what you should send, do a GET on another object & see what Tastypie thinks it should look like.

但是当我通过更改主键来发布相同的数据时,

{
  "address": "/api/v1/address/1",
  "contact_no": "/api/v1/users/8269661606",
  "order_id": "ABCDE",
  "ordered_products": [
    {
      "orders": "/api/v1/orders/ABCDE",
      "price": "40.00",
      "products": "/api/v1/products/1",
      "quantity": 2
    },
    {
      "orders": "/api/v1/orders/ABCDE",
      "price": "70.00",
      "products": "/api/v1/products/2",
      "quantity": 4
    },
    {
      "orders": "/api/v1/orders/ABCDE",
      "price": "67.00",
      "products": "/api/v1/products/3",
      "quantity": 7
    }
  ],
  "payment_method": "COD",
}

我收到回复,

{
  "address": "/api/v1/address/1",
  "contact_no": "/api/v1/users/8269661606",
  "ordered_products": [],
  "payment_method": "COD",
}

模型 OrderedProduct 中的数据未保存。为什么 YYYYYY ??????

最佳答案

试试这个:

def hydrate_m2m(self, bundle):
    for ordered_product in bundle.data['ordered_products']:
        if isinstance(ordered_product, dict):
            ordered_product.update({'orders': bundle.obj})

    return super(OrdersResource, self).hydrate_m2m(bundle)

并从 ordered_products JSON 中删除 orders

并将 attribute=lambda bundle: OrderedProduct.objects.filter(orders=bundle.obj) 替换为 orders

并将 related_name='orders' 从资源移动到模型。

最后:

型号:

class OrderedProduct(models.Model):
    products = models.ForeignKey(Products, on_delete=models.DO_NOTHING)
    orders = models.ForeignKey(Orders, on_delete=models.CASCADE, related_name='orders')
    quantity = models.IntegerField(default=0)
    price = models.DecimalField(default=0.00, max_digits=5, decimal_places=2, blank=False)

资源:

class OrdersResource(ModelResource):

    ordered_products = fields.ToManyField('orders.resources.OrderedProductResource',
                                          'orders', full=True, null=True)

    contact_no = fields.ForeignKey(ProductUsersResource, 'users')
    address = fields.ForeignKey(ProductUsersAddressResource, 'address')

    class Meta:
        queryset = Orders.objects.all()
        resource_name = 'orders'
        include_resource_uri = False
        collection_name = 'orders'
        allowed_methods = ['get', 'post']
        always_return_data = True

    def hydrate_m2m(self, bundle):
        for ordered_product in bundle.data['ordered_products']:
            if isinstance(ordered_product, dict):
                ordered_product.update({'orders': bundle.obj})

        return super(OrdersResource, self).hydrate_m2m(bundle)

和 POST 数据:

{
  "address": "/api/v1/address/1",
  "contact_no": "/api/v1/users/8269661606",
  "order_id": "ABCDE",
  "ordered_products": [
    {
      "price": "40.00",
      "products": "/api/v1/products/1",
      "quantity": 2
    },
    {
      "price": "70.00",
      "products": "/api/v1/products/2",
      "quantity": 4
    },
    {
      "price": "67.00",
      "products": "/api/v1/products/3",
      "quantity": 7
    }
  ],
  "payment_method": "COD",
}

关于python - django-tastypie : Related data not saving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38152123/

相关文章:

python - Wxpython EVT_CHAR 回调未被调用

python - 满足条件的最近 k 个邻居(python)

python - Scrapy:如何提取 HTML 标签内的所有单词?

python - Django 中的显示表单不基于模型/表单

Django 模型(1054, “Unknown column in ' 字段列表'”)

javascript - 按键时运行搜索

python - 在 Python 中添加具有排名排序的附加列

django - Django 模型验证是否仅通过表单 API 处理?

python - GPT-3微调错误: Incorrect API key provided

python - 执行很多问题,MySQLdb