On 8/08/2011 8:42pm, Michal Petrucha wrote:
On Mon, Aug 08, 2011 at 06:28:59PM +1000, Mike Dewhirst wrote:
Can anyone tell me if to_field is valid in this context and what is
the distinction between through and db_table? ...

class Sections(models.Model):
     """ intermediary table for Document recursive m2m """
     document = models.ForeignKey(Document, to_field='ancestor')
     section = models.ForeignKey(Document, to_field='ancestor')
     comment = models.TextField()

class Document(models.Model):
     ancestor = models.IntegerField()
     sections = models.ManyToManyField('self',
                             symmetrical=False,
                             through=Sections,
                             db_table=Sections,
                             related_name='Comprise')
``db_table`` is ignored altogether if you supply a ``through`` model.
Otherwise it is expected to be a string containing the table name.

... where 'ancestor' is a surrogate key which survives in descendant
documents - such being both newer revisions of the same document and
other documents more or less based on the ancestral document.

I also wonder how I might have a m2m recursive relationship between
documents without specifying an intermediary table - iow if the
comment column was not needed - is it OK to specify
to_field='ancestor' in the Document model?
In general, this won't work. ManyToManyField doesn't really support
pointing to any other field than the primary key. That's why it
doesn't have the ``to_field`` option at all.

Note that while the code above might work under some circumstances, it
will break as soon as you start working with ModelForms -- the form
field will feed Document.ancestor with values from Document.pk which
might reault in a complete mess. And that is just one example of what
won't work...

All in all, I recommend redesigning the solution to avoid such usage
of ManyToManyField. I don't really understand what it is you're trying
to achieve but if you are somehow grouping the documents using the
ancestor field, you might try to separate the groups into a new model
or something...

Thanks Michal - doing so will simplify things. I think I can make it more hierarchical.

Cheers

Mike



But again, I don't quite understand what you're doing egen after
re-reading your post from about a week ago...

Michal

--
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