When working with foreign keys, it's really easy to detect changes in the field by writing a custom save method. It would look something like this:
def save(self, force_insert=False, force_update=False): old_user = None if Project.objects.filter(id__exact=self.id).count(): #the project exists project = Project.objects.get(id=self.id) old_user = project.assigned_to #assigned_to field is ForeignKey (User) super(Project, self).save() else: #The project does not exist super(Project, self).save() project = Project.objects.get(id=self.id) new_user = self.assigned_to if old_user != new_user: #the assigned_to user has changed. do something However, after some research and personal testing, it appears you can't do this with ManyToManyField's because the related table is not updated until after the save method is called. In light of this, I wrote a view to detect this change in the front end, but I can't seem to figure out the proper syntax to detect a change in the m2m field. Any feedback on my code would be appreciated. def edit_project(request, project_id): """ Edit a project (marcomm_lead is the M2M field) """ project = get_object_or_404(Project, id=project_id) if request.method == 'POST': form = EditProjectForm(request.POST, instance=project) if form.is_valid(): old_marcomm = project.marcomm_lead.all().values_list('email', flat=True).order_by('email') #store the old marcomm_lead emails new_project = form.save() if new_project.marcomm_lead.all(): #if project has marcomm lead(s) new_marcomm = new_project.marcomm_lead.all().values_list('email', flat=True).order_by('email') #store the new marcomm_lead emails if new_marcomm != old_marcomm: #not sure if this works recipients = [] if len(old_marcomm) > 0: #if there were old marcomm lead(s) for lead in new_marcomm: if lead not in old_marcomm: #never works. please advise! Only add lead to list if it was not in the old marcomm list recipients.append(lead) #add the person's email to the list else: #there were no old marcomm lead(s) for lead in new_marcomm: #for each marcomm lead recipients.append(lead) #add the person's email to the list current_site = Site.objects.get_current() subject = render_to_string('projects/email/ marcomm_lead_subject.txt', { 'project': project }) message = render_to_string('projects/email/ marcomm_lead_body.txt', { 'site': current_site, 'project': project }) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipients) success_url = project.get_absolute_url() return HttpResponseRedirect(success_url) else: form = EditProjectForm(instance=project) return render_to_response('projects/edit_project.html', { 'project': project, 'form': form }, context_instance=RequestContext(request)) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---