这有点奇怪,我得到了这个错误 TypeError: init ()得到一个意外的关键字参数'allow_none' 在DRF 2.4上,它在DRF 2.3.14中运行良好
这似乎与模型中的验证有关,因为我尝试使用ProductImg模型并且它工作正常,所以我知道模型中有问题,我甚至只尝试获取id,但我总是得到相同的错误信息,现在我想我唯一的选择是使用DRF的先前版本,我想在报告错误之前得到一个意见,因为这是我第一次使用DRF
constants.py
GENDER =(
('woman', 'Woman'),
('man', 'Man'),
('none', 'None'),
)
PRODUCT_TYPE =(
('top','Top'),
('bottom','Bottom'),
('dress','Dress'),
('accessories', 'accessories'),
)
serializers.py
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = ('id', 'name')
models.py *
class ProductType(models.Model):
gender = models.CharField(choices=GENDER, max_length=10)
type = models.CharField(max_length=20)
class Category(models.Model):
name = models.CharField(max_length=155, verbose_name=_('Category'))
type = models.ForeignKey(ProductType, related_name='categories_for', verbose_name=_('Category type'))
meta_description = models.TextField(verbose_name=_('Meta Description'))
meta_keywords = models.TextField(verbose_name=_('Meta Keywords'))
parent = models.ForeignKey('Category', related_name='children', verbose_name=_('Category Parent'), blank=True, null=True)
slug = models.SlugField(blank=True, verbose_name=_('URL alias'))
class Trends(models.Model):
name = models.CharField(max_length=255, verbose_name=_('Trends'))
banner = ProcessedImageField(verbose_name=_('Banner'), upload_to='trends', blank=True, processors=[ResizeToFit(width='1200', upscale=False)], format='JPEG', options={'quality': 70})
description = models.TextField(verbose_name=_('Description'))
slug = models.SlugField(blank=True)
pubDate = models.DateTimeField(auto_now_add=True, verbose_name=_('Date published'))
gender = models.CharField(choices=GENDER, max_length=10)
promoted = models.BooleanField(verbose_name=_('Promoted'), default=False)
class Product(models.Model):
reference = models.CharField(max_length=50, verbose_name=_('Reference'))
name = models.CharField(max_length=255, verbose_name=_('Product Name'))
description = models.TextField(blank=True, verbose_name=_('Product Description'))
color_code = models.CharField(max_length=11, verbose_name=_('Color Code'))
color_web = models.CharField(max_length=7, verbose_name=_('Web Color'))
size = models.CharField(max_length=11, verbose_name=_('Product Size'))
price = models.IntegerField(verbose_name=_('Product Price'))
price_promo = models.IntegerField(blank=True, null=True, verbose_name=_('Product Promo Price'))
upc = models.CharField(max_length=155, verbose_name=_('UPC'))
stock = models.IntegerField(verbose_name=_('Product Stock'))
category = models.ForeignKey(Category, verbose_name=_('Estilo'))
in_stores = models.BooleanField(default=True, verbose_name=_('Product Availability'))
try_out = models.BooleanField(default=False, verbose_name=_('Try out Product'))
pub_date = models.DateTimeField(auto_now_add=True)
update = models.DateTimeField(auto_now=True)
slug = models.SlugField(default='', blank=True, verbose_name=_('URL alias'))
blog = models.URLField(blank=True, verbose_name=_('Blog mention link'))
parent_product = models.BooleanField(default=False, verbose_name=_('Main Product'))
active = models.BooleanField(default=True, verbose_name=_('Active'))
trends = models.ManyToManyField(Trends, verbose_name=_('Trends'), blank=True)
photoshoot_id = models.CharField(max_length=255, verbose_name=_('Photoshoot ID'))
回溯
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/jav/vn-azulu/lib/python2.7/site-packages/rest_framework/serializers.py", line 200, in __init__
self.fields = self.get_fields()
File "/Users/jav/vn-azulu/lib/python2.7/site-packages/rest_framework/serializers.py", line 236, in get_fields
default_fields = self.get_default_fields()
File "/Users/jav/vn-azulu/lib/python2.7/site-packages/rest_framework/serializers.py", line 721, in get_default_fields
field = self.get_field(model_field)
File "/Users/jav/vn-azulu/lib/python2.7/site-packages/rest_framework/serializers.py", line 910, in get_field
return ModelField(model_field=model_field, **kwargs)
File "/Users/jav/vn-azulu/lib/python2.7/site-packages/rest_framework/fields.py", line 400, in __init__
super(ModelField, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'allow_none'
答案 0 :(得分:1)
尝试重现问题时:如果删除django model translation内容,则无法重现。
name = models.CharField(max_length=155, verbose_name=_('Category'))
成为
name = models.CharField(max_length=155, verbose_name='Category')
为所有模型执行此操作时,以及刺激数据。数据序列化很好。