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?
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With