0👍
UserProfile
is created after the User
is saved by a post_save
signal. Even if you override it with your own signal, you won’t have access to the form data from there.
The easiest solution is to just allow start_year
to be NULL. It’s not necessary to enforce this at the database level, and you can make the field required in all forms either way:
start_year = models.IntegerField(max_length=4, blank=False, null=True)
Your custom form already enforces that the field is required, so you’re done.
UPDATE (from comment)
Custom form, yes, but you can still use a ModelForm
:
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['start_year'].required = True
UPDATE (again)
Actually, I didn’t think before my last update. I put blank=False
on the start_year
field in my example. That will force that the field is required in all ModelForm
s by default. You don’t need a custom ModelForm
at all. However, I left the previous update for posterity.