views:

53

answers:

1

Hello,

I have the following table in the model with a recursive structure (a page can have children pages)

class DynamicPage(models.Model):    
    name = models.CharField("Titre",max_length=200)
    parent = models.ForeignKey('self',null=True,blank=True)

I want to create another table with ManyToMany relation with this one.

class UserMessage(models.Model):    
    name = models.CharField("Nom", max_length=100)
    page = models.ManyToManyField(DynamicPage)

A UserMessage can be displayed on several pages and a page can display several messages.

When I add a new UserMessage, I have an error due to the following foreign key constraint.

ALTER TABLE `website_dynamicpage` ADD CONSTRAINT `parent_id_refs_id_29c58e1b` FOREIGN KEY (`parent_id`) REFERENCES `website_dynamicpage` (`id`); 

I think that my problem cause is that the constraint is set on the parent field of DynamicPage and not on its id.

Top level DynamicPages don't have a parent page but they can display UserMessage.

So, I would like to have the ManyToMany relationship with the page id and not with the page parent.

I don't see in the Django docs how to say that the ManyToMay is between the UserMessage id and the DynamicPage id rather than the DynamicPage parent.

How to do that? Is it possible?

Is recreating the SQL constraint by hand a possible solution?

Thanks in advance

A: 

(Assuming I understand question correctly ;))

Try this:

class DynamicPage(models.Model):
    #...
    other = models.ManyToManyField("self")

with the optional symmetrical keyword parameter (defaults to True).

http://docs.djangoproject.com/en/dev/ref/models/fields/#manytomanyfield

gorsky