I _think_ your problem might lie in the confusion between "blank" and "null"; taken from https://docs.djangoproject.com/en/dev/ref/models/fields/#blank;
null<https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null> is purely database-related, whereas blank<https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.blank> is validation-related. If a field has blank=True, form validation will allow entry of an empty value. If a field has blank=False, the field will be required. On Tuesday, 25 February 2014 14:17:21 UTC+11, Stefano Crosta wrote: > > Hello! > > I'm trying to understand if this is a bug, or a not clearly documented > feature. > > I have a simple model where one field should be calculated at save. eg. > > class MyModel(models.Model): > a = models.CharField(max_length=255) > b = models.CharField(max_length=255, blank = True) > > Now, if I create an instance in the admin and I try to save leaving `b` > empty (that's ok because it's blank=True) I get a ValueError: > > Cannot assign None: "app.MyModel" does not allow null values. > > Now, I would expect to be able to set `b` to my calculated value in one of > the following places, according to the documentation: > > > - overriding save() in the model class > - in the clean() method in the model class > - with a custom save_model() in the admin class > > Instead, none of those locations are reached, and I get the ValueError > from > > django/db/models/fields/related.py in __set__ > > 1. > > return rel_obj > > 2. > > 3. > > def __set__(self, instance, value): > > 4. > > # If null=True, we can assign null here, but otherwise the value > needs > > 5. > > # to be an instance of the related class. > > 6. > > if value is None and self.field.null == False: > > 7. > > raise ValueError('Cannot assign None: "%s.%s" does not allow > null values.' % > > > > 1. > > (instance._meta.object_name, > self.field.name)) > > > > now, also checking the stack (and even debugging) it seems like this comes > earlier than save. > > > *My question: **Is this somehow expected, is this a bug/a regression?* > > If it's expected, I'll find another workaround; if it's a bug, I can add a > ticket. > > > Thanks! > > Stefano > > > > Stacktrack > > > - /home/3h/django/django/contrib/admin/options.py in add_view > 1. > > if form.is_valid(): > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/forms/forms.py in is_valid > 1. > > return self.is_bound and not bool(self.errors) > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/forms/forms.py in errors > 1. > > self.full_clean() > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/forms/forms.py in full_clean > 1. > > self._post_clean() > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/forms/models.py in _post_clean > 1. > > self.instance = construct_instance(self, self.instance, > opts.fields, opts.exclude) > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/forms/models.py in construct_instance > 1. > > f.save_form_data(instance, cleaned_data[f.name]) > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/db/models/fields/__init__.py in save_form_data > 1. > > setattr(instance, self.name, data) > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > - /home/3h/django/django/db/models/fields/related.py in __set__ > 1. > > (instance._meta.object_name, > self.field.name)) > > ... > ▶ Local vars <http://grigio.vm:8000/admin/ipr/ipr/add/#> > > -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ae35ebac-7937-4239-ae4c-706f09b8fae1%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
