[Django]-Unrequired field error w/ Django REST Framework

2πŸ‘

βœ…

With the help of @user2719875, I figured out the solution. To get the desired behavior – allowing the user to enter a custom string, defaulting to a string generator function if no input is given – I needed to define the default in the serializer itself, not the model. Using the extra_kwargs to select the desired β€˜link’ field, I declare the default function here:

class ClientSerializer(serializers.ModelSerializer):

class Meta:
    model = Client
    fields = ('id', 'name', 'email', 'notes', 'link', 'link_expired', 'created', 'updated')
    extra_kwargs = {
        'link': {
            'default': link_generator
         }
    }

Interestingly, it looks for the link_generator function in models.py itself, so the code is stored there, above the model. Not sure if this is the best practice, but it works. Removed the default=link_generator from the model itself as below:

def link_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


class Client(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField(max_length=254)
    notes = models.TextField(blank=True)
    link = models.CharField(max_length=254)
    link_expired = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)  # added only on creation
    updated = models.DateTimeField(auto_now=True)  # updates each time the object is saved

Working great. Thanks for the help, all!

πŸ‘€jckstl

5πŸ‘

Try changing your serializer to this:

class ClientSerializer(serializers.ModelSerializer):

    class Meta:
        model = Client
        fields = ('id', 'name', 'email', 'notes', 'link', 'link_expired', 'created', 'updated')
        extra_kwargs = {
                'link': {
                    # Tell DRF that the link field is not required.
                    'required': False,
                    'allow_blank': True,
                 }
            }

I’m actually surprised that when you print your serializer, DRF says that the link field is not required because according to your model, it should be required (because blank=True is not set).

Edit: allow_blank is what is used for the CharField serializer fields (If set to True then the empty string should be considered a valid value). Taken from the DRF documentation here: http://www.django-rest-framework.org/api-guide/fields/#charfield

πŸ‘€SilentDev

0πŸ‘

The problem from the first glance that I can see in your code is in default option. It should be function itself(without brackets). Change it to something like this:

class Client(models.Model):
    link = models.CharField(max_length=254, default=link_generator)

Hope it helps!

πŸ‘€Alexander B.

0πŸ‘

My code is

class AccountSerializer(serializers.ModelSerializer):

    users = serializers.CharField(write_only=True, required=False)
    name = serializers.CharField(write_only=True)

when executed: for, users=” , getting serializer error required
field.

Here required=False & blank=True not working,

I want to allow also blank values..

Leave a comment