Django Rest Framework 2.4 TypeError: __init__() go

2019-03-21 11:04发布

问题:

This is kind of weird, I get this error TypeError: init() got an unexpected keyword argument 'allow_none' on DRF 2.4 but it works fine in DRF 2.3.14

It seems to be something with the validation in the model because I tried with the ProductImg Model and it works fine, so I know there is something wrong in the model, I even tried only getting the id, but I always get the same error message, for now I guess my only option is to use DRF's previous version, I would like to get an opinion before reporting a bug, as it is the first time I use 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

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'

回答1:

When trying to reproduce the issue: I cannot reproduce it, if I remove the django model translation stuff.

name = models.CharField(max_length=155, verbose_name=_('Category'))

becomes

name = models.CharField(max_length=155, verbose_name='Category')

When doing this for all of your models, and stimulating data. The data serializes just fine.