The solution was that I needed to set symmetrical=False, per the documentation here: https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ManyToManyField.symmetrical
On Tuesday, July 19, 2016 at 2:52:16 PM UTC-4, Farhan Khan wrote:
-- On Tuesday, July 19, 2016 at 2:52:16 PM UTC-4, Farhan Khan wrote:
I am using a ManyToManyField whose target is a 'self' reference, but it does not create a related_name "reverse" field.Here is my model:
class SecurityGroup(models.Model):
name = models.CharField(max_length=100 )
description = models.TextField()
subgroups = models.ManyToManyField('self', related_name='origin')
def __str__(self):
return self.name
And here is the problem:
>>> x = SecurityGroup(name='name', description='description')
>>> x.save()
>>> x.origin()
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'SecurityGroup' object has no attribute 'origin'
There also no x.securitygroup_set. Is this a bug?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8076eb42-3604-43ad-aec2-94d04d0c6602%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment