0
因此,在Django中,.create()不支持可寫嵌套字段。但是,我的項目中有一個嵌套字段。我看了一下this question,這很有幫助,但是我現在在/ transactions/ 處得到一個ValueError,用於int(),其基數爲10:'Product001'。據我所知,這是由線serializer.py讀取django - making .create()支持可寫的嵌套字段?
product = Product.objects.get(pk=validated_data.pop('sku'))
具體而言,「SKU」值我在那裏造成的。我的問題是,我應該在那裏置換'sku'的價值?這個問題的答案是基於我的代碼使用「事件」,但這不是我項目中模型的一部分。我也嘗試使用'產品',並得到了一個TypeError,sadi「int()參數必須是字符串,類似字節的對象或數字,而不是'collections.OrderedDict'」。
serializers.py
from .models import Product, Family, Location, Transaction
from rest_framework import serializers
class LocationSerializer(serializers.ModelSerializer):
class Meta:
model = Location
fields = ('reference', 'title', 'description')
class FamilySerializer(serializers.ModelSerializer):
class Meta:
model = Family
fields = ('reference', 'title', 'description', 'unit', 'minQuantity')
class ProductSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Product
fields = ('sku', 'barcode', 'title', 'description', 'location', 'family')
depth = 1
class TransactionSerializer(serializers.ModelSerializer):
product = ProductSerializer()
class Meta:
model = Transaction
fields = ('sku', 'barcode', 'product')
def create(self, validated_data):
product = Product.objects.get(pk=validated_data.pop('sku'))
instance = Transaction.objects.create(**validated_data)
return instance
def to_representation(self, instance):
representation = super(TransactionSerializer, self).to_representatio
n(instance)
return representation;
models.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)