60👍
Yeah, I ran into this issue at some point as well. You need to also update the validation exclusions.
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(required=False)
class Meta:
model = models.FavoriteList
def get_validation_exclusions(self):
exclusions = super(FavoriteListSerializer, self).get_validation_exclusions()
return exclusions + ['owner']
41👍
Late Entry to this thread. This issue was fixed in django-rest-framework 2.3.13. Here is the link of the PR.
You use it like this in your case:
class Meta:
model = models.FavoriteList
optional_fields = ['owner', ]
- [Django]-How do I reuse HTML snippets in a django view
- [Django]-Suppress "?next=blah" behavior in django's login_required decorator
- [Django]-Setting Django up to use MySQL
37👍
In case somebody lands here with a similar issue, pay attention to the following attributes along with required
:
If set to
True
then the empty string should be considered a valid value.
Normally an error will be raised if
None
is passed to a serializer field.
Normally an error will be raised if a field is not supplied during deserialization.
I was straggling to figure out why I was getting a validation error with required=False
where I had missed the allow_null
attribute.
- [Django]-Navigation in django
- [Django]-Setting DEBUG = False causes 500 Error
- [Django]-SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend
22👍
In 2020, for DRF 3.12.x, the approach that I prefer the approach that relies on
Serializer’s extra_kwargs.
So assuming your
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(required=False)
class Meta:
model = models.FavoriteList
fields = ["owner"] # and whatever other fields you want to expose
extra_kwargs = {"owner": {"required": False, "allow_null": True}}
- [Django]-Django Rest Framework remove csrf
- [Django]-AngularJS with Django – Conflicting template tags
- [Django]-Django: no such table: django_session
4👍
If you have unique_together constraint on one of the fields you are trying to set required=False
you need to set validators=[]
in serializers Meta like
class FavoriteListSerializer(serializers.ModelSerializer):
owner = serializers.IntegerField(required=False)
class Meta:
model = models.FavoriteList
validators = []
Here is the original answer
- [Django]-Using Django time/date widgets in custom form
- [Django]-Dynamically adding a form to a Django formset
- [Django]-ImportError: cannot import name 'six' from 'django.utils'
1👍
You can also do this:
class ASerializer(serializers.HyperlinkedModelSerializer):
owner = serializers.HiddenField(default=serializers.CurrentUserDefault())
...
As referred here: https://www.django-rest-framework.org/api-guide/validators/#advanced-field-defaults
There you can also find the case when you also wanna let the view show owner
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
- [Django]-Django CSRF Cookie Not Set
- [Django]-Django – Static file not found
0👍
I would set model field to allow null value (and possible also default to None)
class FavoriteList(models.Model):
owner = models.PositiveIntegerField(null=True, default=None)
Then it’s possible to just leave owner field to Meta section. These fields, without any extra settings, will automatically get all attributes from model field and be non-required.
class FavoriteListSerializer(serializers.ModelSerializer):
class Meta:
model = models.FavoriteList
fields = ('owner',)
- [Django]-Update only specific fields in a models.Model
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-Django project base template