django - 使.create()支持可写的嵌套字段?

时间:2017-10-15 15:40:42

标签: python django

所以在Django中,。create()不支持可写的嵌套字段。但是,我的项目中有一个嵌套字段。我看了this question,这很有帮助,但我现在在/ transactions /得到一个ValueError int()的基数为10的无效文字:'Product001'。据我所知,这是由 serializer.py 中的行读取

引起的
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Product(models.Model):

    sku = models.CharField(max_length=13,help_text="Enter Product Stock Keeping Unit")
    barcode = models.CharField(max_length=13,help_text="Enter Product Barcode (ISBN, UPC ...)")

    title = models.CharField(max_length=200, help_text="Enter Product Title")
    description = models.TextField(help_text="Enter Product Description")

    unitCost = models.FloatField(help_text="Enter Product Unit Cost")
    unit = models.CharField(max_length=10,help_text="Enter Product Unit ")

    quantity = models.FloatField(help_text="Enter Product Quantity")
    minQuantity = models.FloatField(help_text="Enter Product Min Quantity")

    family = models.ForeignKey('Family')
    location = models.ForeignKey('Location')

    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Product.
        """
        return reverse('product-detail-view', args=[str(self.id)])

    def __str__(self):

        return self.title

class Family(models.Model):

    reference = models.CharField(max_length=13, help_text="Enter Family Reference")
    title = models.CharField(max_length=200, help_text="Enter Family Title")
    description = models.TextField(help_text="Enter Family Description")

    unit = models.CharField(max_length=10,help_text="Enter Family Unit ")

    minQuantity = models.FloatField(help_text="Enter Family Min Quantity")

    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Family.
        """
        return reverse('family-detail-view', args=[str(self.id)])

    def __str__(self):

        return self.title

class Location(models.Model):

    reference = models.CharField(max_length=20, help_text="Enter Location Reference")
    title = models.CharField(max_length=200, help_text="Enter Location Title")
    description = models.TextField(help_text="Enter Location Description")

    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Location.
        """
        return reverse('family-detail-view', args=[str(self.id)])

    def __str__(self):

        return self.title

class Transaction(models.Model):

    sku = models.CharField(max_length=13,help_text="Enter Product Stock Keeping Unit")
    barcode = models.CharField(max_length=13,help_text="Enter Product Barcode (ISBN, UPC ...)")

    comment = models.TextField(help_text="Enter Product Stock Keeping Unit")

    unitCost = models.FloatField(help_text="Enter Product Unit Cost")

    quantity = models.FloatField(help_text="Enter Product Quantity")

    product = models.ForeignKey('Product')

    date = models.DateField(null=True, blank=True)

    REASONS = (
        ('ns', 'New Stock'),
        ('ur', 'Usable Return'),
        ('nr', 'Unusable Return'),
    )


    reason = models.CharField(max_length=2, choices=REASONS, blank=True, default='ns', help_text='Reason for transaction')

    def get_absolute_url(self):
        """
        Returns the url to access a particular instance of Product.
        """
        return reverse('transaction-detail-view', args=[str(self.id)])

    def __str__(self):

        return 'Transaction :  %d' % (self.id)

具体来说,我在那里的'sku'价值。我的问题是,我应该用什么价值来代替'sku'?问题的答案我将我的代码基于使用的'事件',但这不是我的项目中的模型的一部分。我也试过使用'product',并得到一个TypeError,其中sadi“int()参数必须是字符串,类似字节的对象或数字,而不是'collections.OrderedDict'”。

serializers.py

-R

models.py

> jshell -v -R-Da=b ./file.jsh

1 个答案:

答案 0 :(得分:0)

您可以找到有关可写嵌套序列化here的详细信息。

关于您的情况,您可以从product dict获取产品的def create(self, validated_data): product_data = validated_data.pop('product') try: product = Product.objects.get(pk=product_data.get('sku')) except Product.DoesNotExist: product = Product.objects.create(**product_data) instance = Transaction.objects.create(**validated_data) instance.product = product instance.save() return instance 数据:

{{1}}