I am building a database system using Admin. Without patching Django I
have found a way to prevent deleting records with other records
associated with them.  This is the same problem addressed by ticket
#1007. http://code.djangoproject.com/ticket/1007

If we have a model:

class Nlgroup(models.Model):
  description = models.CharField(maxlength=80)

class People(models.Model):
  nl_group = models.ForeignKey(Nlgroup)

I want to prevent deletion of Nlgroup records if there are People
records that are associated with the group.

Here is how to do it. Override the delete function of Nlgroup

class Nlgroup(models.Model):
  description = models.CharField(maxlength=80)
  def delete(self):
    if not People.objects.filter(nl_group__exact=self).count():
      super(Nlgroup, self).delete() # Call the "real" delete()
method

class People(models.Model):
  nl_group = models.ForeignKey(Nlgroup)

This does offend the DRY principle, but until Django has a prettier
way of handling this, I will put up with the duplication.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to