-
-
Save SmileyChris/4438999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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