2

If I define a Django model with a recursive relationship as follows,

Class M(models.Model):
    a = models.CharField(max_length=1)
    ms = models.ManyToManyFied('self')

and in the admin site I enter data such afterwards M.a = 'a','b', and 'c'. The Django admin site's change form will show ms to contain 'a', 'b', 'c'. How do I remove 'a' from the list since I don't want it assigned to current M.a which is itself?

Devone
  • 155
  • 1
  • 1
  • 12

1 Answers1

1

This is not a direct answer to your question, but it may be a good practical solution. You can create a ModelForm that validates your data (i.e., prevents a model from setting a relationship with itself), then customize your model's admin class to use that custom form in the admin site. This SO post describes how in detail: Django ManyToMany model validation

Community
  • 1
  • 1
souldeux
  • 3,615
  • 3
  • 23
  • 35
  • this is a good practical solution thanks. But like you said it does not answer my question directly, still you're solution is helpful. – Devone Aug 10 '16 at 15:37