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... But again, I don't quite understand what you're doing egen after re-reading your post from about a week ago... Michal
signature.asc
Description: Digital signature