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.
class JobForm(forms.ModelForm):
""" this handles the start_time and end_time defaults"""
class Meta:
exclude = ['slug','author',]
model = Job
def save(self, *args, **kw):
last_entry = Job.objects.filter(author=self.request.user).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)
################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")
@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('job/job_form.html', { 'form': form })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment