Why is this many-to-many relation changing whitout me asking for it, using the Django ORM?
I have a recursive M2M relationship with an indicator class. An indi开发者_如何转开发cator is a int, or a calculated value from 2 indicators:
class Indicator(models.Model):
params = models.ManyToManyField('self',
verbose_name=__(u'parameters'),
related_name='params_of',
blank=True, null=True)
type = models.CharField(max_length=64, verbose_name=__(u'type'))
def get_value(self, record):
# etc
According to the type, get_value doesn't do the same things. It can just return the numerical value or calculate a value from the numerical value of each parameters from the params
attribute. As you can see params
is a recursive m2m relationship.
Now my problem is that, I have the follwoing indicators:
- men
- women
- total pop
- men ratio
If I add men
and women
as parameters of total pop
, everything is fine, and I get my total pop dynamically calculated. But if I then add men
and total pop
as parameters to men ratio
, then total pop
automatically get men ratio
as a parameter. It breaks everything.
Why is that? How can I avoid it?
M2M relations in Django are symmetric by default ie in an M2M, if a related to b then b also related to a. To stop this, use symmetrical=False
your params would become:
params = models.ManyToManyField('self',
verbose_name=__(u'parameters'),
related_name='params_of',
blank=True, null=True,
symmetrical=False)
精彩评论