Summary: django should throw an exception when a model has its own
name as the 'to' field of a ManyToMany() insead of 'self'.

I had a field like this:

    def Item(Model):
        name = CharField(max_length=100)
        related_items = ManyToManyField('Item', blank=True)

But this caused some very strange bugs.  For example:

    item1 = Item.objects.create(name='First')
    item1.save()

    item2 = Item.objects.create(name='Second', related_items=[item1])
    item2.save()

    print item2.related_items
    > []
    print len(item2.related_items)
    > 0

The correct way, as stated in the docs, is to use 'self' for a
recursive relationship.

    def Item(Model):
        name = CharField(max_length=100)
        related_items = ManyToManyField('self', blank=True)

(NOTE: This is pseudo/test code, may contain typos)

When I use this method, it works fine.  However, if the first method
is incorrect and produces functionally invalid results, then there
should be a model validation check for this and an exception if it's
found.  Basically, if a model has a ManyToManyField() with its own
name as the 'to' parameter, an exception should be raised.

I understsand that the current functoinality & docs are working as
they're supposed to.  However, it would also be nice for django to
raies an exception instead of silently failing and producing invalid
results.

Have I missed anything?  I may be misunderstanding the problem here,
but I have managed to fix my problem and figure this could save others
from the same trap.  I'd like to get some input before submitting a
ticket.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to