Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
Forked from thebookworm101/my integrity
Last active December 10, 2015 13:08
Show Gist options
  • Save SmileyChris/4438999 to your computer and use it in GitHub Desktop.
Save SmileyChris/4438999 to your computer and use it in GitHub Desktop.
################entering some data the first time to my form doesnt work, it gives this error:
IntegrityError at /job/add/article/
(1048, "Column 'author_id' cannot be null")
Request Method: POST
Request URL: http://localhost:8000/job/add/article/
Django Version: 1.4.3
Exception Type: IntegrityError
Exception Value:
(1048, "Column 'author_id' cannot be null")
class JobForm(forms.ModelForm):
"""
A form used for creating a Job. It handles the start_time and end_time defaults.
"""
class Meta:
fields = [] # Everything except 'slug' and 'author'.
model = Job
def save(self, *args, **kw):
last_entry = self.instance.author.job_set.aggregate(max_end_time=Max('end_time'))['max_end_time']
if last_entry:
self.instance.start_time = last_entry.end_time
return super(JobForm, self).save(*args, **kw)
@login_required
def add_job(request):
form = JobForm(data=request.POST or None)
if form.is_valid():
form.instance.author = request.user
article = form.save()
msg = "Job saved successfully"
messages.success(request, msg, fail_silently=True)
return redirect(article)
return render(request, 'job/job_form.html', {'form': form})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment