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!
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
- [Django]-Django send_mail get result
- [Django]-Django QuerySet querying or filtering "Odd" and/or "Even" value in a particular field
- [Django]-No migrations to apply
- [Django]-Csrf issue from node js to django
- [Django]-Why Django translation does not use the base language in javascript?
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!
- [Django]-What is the best way to consume a django-piston REST API from a Django view?
- [Django]-Django β how to run it on a production server?
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..
- [Django]-Django XFrameOptionsMiddleware (X-Frame-Options) β allow iframe by client IP
- [Django]-Can't get media files in heroku
- [Django]-Pass errors in Django using HttpResponseRedirect
- [Django]-Disable django context processor in django-admin pages
- [Django]-How to send emails from django App in Google App Engine