I have a training application I wrote using Django.
Users can express interest in a course and then enroll in an offering
of that course.
I have been overriding the save() and delete() methods of my
Enrollment and Interest models so that that following happens.

1. When a user enrolls in an offering of a particular course, their
interest in that course if removed.
2. When a user un-enrolls from an offering their interest is restored.

The problem now it that when I delete an Offering object through the
Django admin it cascade deletes the Enrollment objects as it should
but it is not calling the delete method and thus restoring their
interest.

When deleting an Enrollment through the admin it calls the delete
method but not on cascades.

Is it documented anywhere that this is the behavior?  I noticed that
the post_save signal is being dispatched on each of those so I could
re-factor my code.  But I'm curious... is it documented?  Is this a
bug?

Here is the code.

#
# models have been stripped down for posting on django-users.
#

class Offering(models.Model):

    course            = models.ForeignKey(Course,
                        help_text='Course')
    date              = models.DateField(
                        help_text='Date')
    attendees         = models.ManyToManyField(User,
through='Enrollment',
                        help_text='Attendees')

class Enrollment(models.Model):
    '''This is a relation table between an offering and a user'''

    # relation fields
    user     = models.ForeignKey(User)
    offering = models.ForeignKey(Offering)

    class Meta:
        # this is automatic on a ManyToManyField but not when we have
        # a custom relation table defined via 'through' like this one
        unique_together = ('user', 'offering')

    def save(self, *args, **kwargs):
        ret = super(Enrollment, self).save(*args, **kwargs)
        # after enrollment, remove their interest from the course
        Interest.objects.filter(user=self.user,
course=self.offering.course).delete()
        return ret

    def delete(self):
        # re-create interest
        interest = Interest(course=self.offering.course,
user=self.user)
        interest.save()
        return super(Enrollment, self).delete()

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.


Reply via email to