On Dec 17, 2007 7:05 PM, radioflyer <solidether gmail.com> wrote:
> I have a Student model with a Foreign Key relationship
to Teacher.
> When a teacher is removed, so go the students. Too
dangerous for my
> particular scenario. There doesn't seem to be a Django
feature that
> allows for adjusting the cascade.
Personally, I'd keep the ForeignKey relationship, but just
override
Teacher.delete() so that removes itself from all of its
Students
before being deleted. Something like this should do it:
class Teacher(models.Model):
# ... Fields here
def delete(self):
for student in self.student_set.all():
student.teacher = None
student.save()
super(Teacher, self).delete()
This, of course, assumes that you created your students
include a
field looking something like this:
teacher = ForeignKey(Teacher, null=True)
Would that work for you?
-Gul
--~--~---------~--~----~------------~-------~--~----~
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-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|