I have a user facing form, which is generated from a model. The model has a foreign key to a site “Section.” When I submit the form it returns an IntegrityError (null value in column “section_id” violates not-null constraint). I thought the issue was straightforward until I started working on it. The bug only occurs on my development server (CentOS release 5.5 (Final)). I don’t have this issue on my local environment (Mac OS X 10.5.8).
I entered the python shell and examined the form object; I noticed the “section” field (form.fields.section) was missing on my dev env. Both the project code and django version are identical on my local and dev. Adding to my frustration is the fact that the user facing form does have a hidden “section” field, but when that data is posted to the view the form field no longer exists. Any insight into the cause of this bug would be greatly appreciated and save me from the expense of Rogaine.
Thanks in advance,
Chris.
Code:
views.py
def event_registration(request, slug):
section = get_object_or_404(Section, section__slug=slug)
parent_page = Page.objects.get(section=section, slug='overview')
page = Page.objects.get(section=section, slug='events-registration')
form = EventRegistrationForm(label_suffix='')
submitted = False
if request.POST.has_key('event'):
form = EventRegistrationForm(request.POST)
if form.is_valid():
form.save()
submitted = True
context = RequestContext(request)
return render_to_response('section/event_registration.html',
{
'section': section,
'page': page,
'form': form,
'submitted': submitted
},
context_instance=context)
forms.py
class EventRegistrationForm(better_forms.BetterModelForm):
class Meta:
model = EventRegistration
fieldsets = [
('Step 1', {
'fields': ['event', 'guests']
}),
('Step 2', {
'fields': ['first_name', 'email', 'last_name', 'phone', 'school_name']
}),
]
models.py
class EventRegistration(models.Model):
section = models.ForeignKey(Theater, verbose_name=_('Section'),
help_text=_("The Site Section"))
event = models.ForeignKey(Event, verbose_name=_('Event'))
guests = models.IntegerField(_('Number of Guests?'))
first_name = models.CharField(_('First Name'), max_length=100)
last_name = models.CharField(_('Last Name'), max_length=100)
email = models.CharField(_('E-Mail Address'), max_length=255)
phone = PhoneNumberField(_('Daytime Phone Number'))
school_name = models.CharField(_('School Name'), max_length=200, blank=True, null=True)
Try this for your view:
edit:
technically, if you had just changed
that would have also worked. You needed to fill out the section field of the new EventRegistration before committing it to the db.