Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign default value to Django model Integerfield, while related FormField is optional?

Tags:

I want to create a Django model Field (IntegerField) with a default value, and also create a form derived from the model, where the field is optional. If it's not set on the form, then when I save the form, I want the default value saved to the DB.

# model.py class Invoice(models.Model):     # IntegrityError "Column 'expireDays' cannot be null"     expireDays = models.PositiveSmallIntegerField(default=1)      # expireDays = *null* in DB     expireDays = models.PositiveSmallIntegerField(default=1, blank=True, null=True)  # forms.py class InvoiceForm(forms.ModelForm):     # leaving this line out gives invalid form     expireDays = forms.IntegerField(required=False)     class Meta:         model = Invoice 

(I used only one of the field declaration lines at a time. :)

I'm not even sure that I'm declaring the default value correctly. The only reference I could find to it was in an article on handling choices by James Bennett. I have yet to find it in the Django docs (I'm using version 1.2 - maybe it's in 1.3?)

Update - I tried setting the field's default value in the MySql database, to no effect. It seems as if, even when the form does not have a value for the field, it goes ahead and assigns null to the DB, over-riding the MySql default value.

Although I am currently just setting a default value in the view that creates the form - I don't really like that, since it puts the responsibility for the field's integrity in the view, not the DB.

The way I would have thought it would work, is that the field could be set, or not, in the form - if set, that value would be written to the DB, and if not set, the DB default would be used. Instead, if not set, the form is writing a null to the DB. So what's the point of having a default value in the ModelField declaration if it's not used? What exactly does it do?

like image 611
John C Avatar asked May 15 '11 22:05

John C


People also ask

What is null true in Django models?

If a string-based field has null=True , that means it has two possible values for “no data”: NULL , and the empty string. In most cases, it's redundant to have two possible values for “no data;” the Django convention is to use the empty string, not NULL .

What is related name in Django models?

The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name, Django automatically creates one using the name of your model with the suffix _set.

What is AutoField in Django?

According to documentation, An AutoField is an IntegerField that automatically increments according to available IDs. One usually won't need to use this directly because a primary key field will automatically be added to your model if you don't specify otherwise.


1 Answers

i you want field to be optional - just leave second definition in the model and do not add anything in the form definition:

class Invoice(models.Model):     expireDays = models.PositiveSmallIntegerField(default=1, blank=True, null=True)  class InvoiceForm(forms.ModelForm):     class Meta:         model = Invoice 

update, so in case there is no value set, use 1 as the field value:

class InvoiceForm(forms.ModelForm):      def clean_expireDays(self):         exp_days = self.cleaned_data.get('expireDays')         if exp_days is None:             return self.fields['expireDays'].initial             # above can be: return 1             # but now it takes value from model definition         else:             return exp_days      class Meta:         model = Invoice 
like image 93
Jerzyk Avatar answered Nov 09 '22 03:11

Jerzyk