【发布时间】:2018-03-27 04:18:12
【问题描述】:
所以在 Django 中,.create() 不支持可写嵌套字段。但是,我的项目中有一个嵌套字段。我查看了this question,这很有帮助,但我现在在 /transactions/ 处遇到了 ValueError 基数为 10 的 int() 的无效文字:“Product001”。据我所知,这是由 serializer.py 中的
行引起的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)
【问题讨论】: