Sunday 15 April 2012

Displaying many-to-many (with through) and self-referencing relationship in Django -


I set up a self-referenced model in Django 1.5 like this:

  RELATIONSHIP_PARENT = 1 RELATIONSHIP_BLOCKED = 2 RELATIONSHIP_STATUSES = ((RELATIONSHIP_PARENT, 'Parent'), (RELATIONSHIP_BLOCKED, 'block')) class message (models.Model): content = models.CharField ( "content", MAX_LENGTH = 160, db_index = true) relationships = models.ManyToManyField ( 'self', through = 'Relationship', symmetric = false, related_name = 'related_to') class relationship (models.Model): parent_message = models.ForeignKey (message, related_name = 'parent_messages') Child_message = models.ForeignKey (message, related_name = 'child_messages') status = models.Intege RField (base = RELATIONSHIP_STATUSES)   

This post is inspired by "asymmetrical relationship - Twitter model" that they describe (my goal is to make the relationship between parents among the messages, but Maybe there is irrelevant information for this question). I was trying to configure the Django Admin page to show the message section under the relationship section. I adhere to what I seem to be the closest example in Django documentation to try as shown below:

  import messages from django.contrib import administrator demo.models, Relationship category RelationshipInline (admin .TabularInline): model = Relationship additional = 1 square MessageAdmin (admin.ModelAdmin): inlines = (RelationshipInline,) admin.site.register (message, MessageAdmin) admin.site.register (Relationship)   

But I get an error like this:

  exception / admin /   

I know that my case is slightly different from the example of the Django documentation, I have a self-referenced model in it. I would like to know whether one of the messages to show a particular message Method (called current child and parent in a scene / page via advanced console) If so, can anyone tell me how to do this? I am new to Django and am still learning, so apologies if this question is very simple. Thank you.

try it

  class relationship inline (admin.TabularInline): Model = Relative Extra = 1 fk_name = 'parent_message' depends on # or 'child_message', which you want to include   

You need to determine that out of 2 Which FK in this case

No comments:

Post a Comment