-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only map 'allow_blank' keyword for fields that support it #3011
Comments
What's a good way for us to be sure about or review exactly the full set of built-in fields that support |
Can't we define allow_blank at the class level - in |
DRF maps that attribute on a django model to an The two main options I see are:
|
Hi, I figured, that since drf-haystack is solely used for reading data, some field attributes such as It's not ideal though, I'd rather not have this list of fields to ignore, so thumbs up if this is getting fixed =) |
Closed by #3012. |
Brief
Started using https://github.com/inonit/drf-haystack and it exposed an issue with the way
field.blank
gets mapped toallow_blank
inModelSerializer
. Specifically, it is added for all fields instead of only the fields that actually support the keyword (CharField
andChoiceField
I believe). This causes an error if one were to try to instantiate a different field type with the result ofrest_framework.utils.field_mapping.get_field_kwargs
(like so: https://github.com/inonit/drf-haystack/blob/master/drf_haystack/serializers.py#L114-L115)Detail
The stack trace I ran into is at the bottom, but here's why it happens:
There is a model like so:
It is conceivable that one might have such a model field definition if they were using it to support both a DRF API and regular Django form validation.
The
blank=True
causes the code infield_mapping.py
(https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/utils/field_mapping.py#L105-L106) to add theallow_blank
keyword to the field kwargs.So when drf_haystack gets the field kwargs and tries to instantiate a
BooleanField
,Field.__init__
throwsTypeError: __init__() got an unexpected keyword argument 'allow_blank'
becauseserializers.BooleanField
doesn't pop it off the kwargs likeserializers.CharField
andserializers.ChoiceField
do before passing the kwargs up.Solutions
There are, of course, workarounds. drf-haystack could hack something up. I could not use
blank=True
and do some custom form handling, but this just exposed something I think should be a part of DRF behavior. If 'allow_blank` is going to be added for all field types, then all field types should accept it (and optionally, ignore it) or it should only be added for field types that support it.I'd be happy to throw together a PR if you agree. I'm inclined to just check for instance of
models.CharField
before setting the keywordstacktrace if you're interested
The text was updated successfully, but these errors were encountered: