Django CreateView:如何在保存时执行操作 [英] Django CreateView: How to perform action upon save
问题描述
我正在使用自定义的CreateView(CourseCreate)和UpdateView(CourseUpdate)保存和更新课程。保存课程后,我想采取措施。我将在新课程的讲师和用户之间建立新的多对多关系(如果尚不存在)。
I'm using a custom CreateView (CourseCreate) and UpdateView (CourseUpdate) to save and update a Course. I want to take an action when the Course is saved. I will create a new many-to-many relationship between the instructor of the new course and the user (if it doesn't already exist).
因此,我想将课程另存为课程,然后使用course.faculty创建新关系。做到这一点的最佳地点在哪里?
So, I want to save the Course as course, and then use course.faculty to create that new relationship. Where is the best place to make this happen?
我试图在视图中的form_valid中执行此操作,但是在尝试访问form.instance.faculty bc时出现错误,未创建课程还没有(在CourseCreate中)。错误消息如下:
I'm trying to do this in form_valid in the views, but I'm getting errors when trying to access form.instance.faculty bc the course isn't created yet (in CourseCreate). The error message is like:
课程:...必须具有字段 course的值,然后才能使用这种多对多关系。
"Course: ..." needs to have a value for field "course" before this many-to-many relationship can be used.
它在CourseUpdate中也不起作用。助手关系未创建。我应该在表格中尝试吗?但是我不确定如何将用户信息发送到表单。
谢谢。
It's also not working in CourseUpdate. The Assists relationship is not created. Should I be trying this in the Form? But I'm not sure how to get the user info to the Form. Thank you.
models.py
models.py
class Faculty(models.Model):
last_name = models.CharField(max_length=20)
class Course(models.Model):
class_title = models.CharField(max_length=120)
faculty = models.ManyToManyField(Faculty)
class UserProfile(models.Model):
user = models.OneToOneField(User)
faculty = models.ManyToManyField(Faculty, through='Assists')
class Assists(models.Model):
user = models.ForeignKey(UserProfile)
faculty = models.ForeignKey(Faculty)
views.py
class CourseCreate(CreateView):
model = Course
template_name = 'mcadb/course_form.html'
form_class = CourseForm
def form_valid(self, form):
my_course = form.instance
for f in my_course.faculty.all():
a, created = Assists.objects.get_or_create(user=self.request.user.userprofile, faculty=f)
return super(CourseCreate, self).form_valid(form)
class CourseUpdate(UpdateView):
model = Course
form_class = CourseForm
def form_valid(self, form):
my_course = form.instance
for f in my_course.faculty.all():
a, created = Assists.objects.get_or_create(user=self.request.user.userprofile, faculty=f)
return super(CourseUpdate, self).form_valid(form)
推荐答案
form_valid()
方法用于 CreateView
和 UpdateView
保存表单,然后重定向到成功URL。 返回super()
是不可能的,因为要在被保存的对象和重定向之间进行操作。
The form_valid()
method for CreateView
and UpdateView
saves the form, then redirects to the success url. It's not possible to do return super()
, because you want to do stuff in between the object being saved and the redirect.
第一个选择是不调用 super()
,并在视图中复制两行。这样做的好处是非常清楚发生了什么。
The first option is to not call super()
, and duplicate the two lines in your view. The advantage of this is that it's very clear what is going on.
def form_valid(self, form):
self.object = form.save()
# do something with self.object
# remember the import: from django.http import HttpResponseRedirect
return HttpResponseRedirect(self.get_success_url())
第二种选择是继续调用 super()
,但在更新关系之前,不要返回响应。这样做的好处是您没有复制 super()
中的代码,但是缺点是不清楚所发生的事情,除非您熟悉 super()
。
The second option is to continue to call super()
, but don't return the response until after you have updated the relationship. The advantage of this is that you are not duplicating the code in super()
, but the disadvantage is that it's not as clear what's going on, unless you are familiar with what super()
does.
def form_valid(self, form):
response = super(CourseCreate, self).form_valid(form)
# do something with self.object
return response
这篇关于Django CreateView:如何在保存时执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!