我想在 .save() 之前和之后比较一个字段(manytomany),以了解哪些条目已被删除。我试过了:
def save(self):
differentiate_before_subscribed = Course.objects.get(id=self.id).subscribed.all()
super(Course, self).save() # Call the "real" save() method.
differentiate_after_subscribed = Course.objects.get(id=self.id).subscribed.all()
#Something
但是 distinct_before_subscribed 和 distinct_after_subscribed 具有相同的值。我必须使用信号?如何?
编辑 :
def addstudents(request, Course_id):
editedcourse = Course.objects.get(id=Course_id) # (The ID is in URL)
# Use the model AssSubscribedForm
form = AddSubscribedForm(instance=editedcourse)
# Test if its a POST request
if request.method == 'POST':
# Assign to form all fields of the POST request
form = AddSubscribedForm(request.POST, instance=editedcourse)
if form.is_valid():
# Save the course
request = form.save()
return redirect('Penelope.views.detailcourse', Course_id=Course_id)
# Call the .html with informations to insert
return render(request, 'addstudents.html', locals())
# The course model.
class Course(models.Model):
subscribed = models.ManyToManyField(User, related_name='course_list', blank=True, null=True, limit_choices_to={'userprofile__status': 'student'})