In my models.py
class Alert(models.Model):
user = models.CharField(max_length=30, blank=True)
a = models.IntegerField(blank=True)
def __unicode__(self):
return "'%s' at %s" % (self.user)
In My forms.py:
class AlertForm(forms.ModelForm):
class Meta:
model=Alert
fields = ('a','user')
widgets = {
'user': forms.HiddenInput()
}
AlertCountFormset = modelformset_factory(Alert,
form = AlertForm)
In my views.py:
def profile_setting(request, slug):
if request.method == 'GET':
form = AlertForm(request.POST)
if form.is_valid():
alert_form = form.save(commit=False)
alert_form.user = request.user.username
alert_form.save() # Here i am getting the error
return HttpResponseRedirect('/user/list')
extra_context = {
'form': AlertForm()
}
return direct_to_template(request,'users/profile_setting.html',
extra_context)
I am trying to fill the Django model Form but i am getting following error where i had put the comment:
events_alertcount.a may not be NULL
What is this? Even after putting thenull=True in the field of a it shows an same error. Is that something wrong with my forms.py or models.py?
This is enforced on database level, too. Set your “a” column in your db to allow the field to be NULL. This should fix it. HTH.