ManyToMany to_field through and db_table questions

270 views
Skip to first unread message

Mike Dewhirst

unread,
Aug 8, 2011, 4:28:59 AM8/8/11
to django...@googlegroups.com
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')

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

Thanks for any help

Mike

Michal Petrucha

unread,
Aug 8, 2011, 6:42:49 AM8/8/11
to django...@googlegroups.com
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

Mike Dewhirst

unread,
Aug 8, 2011, 8:16:48 PM8/8/11
to django...@googlegroups.com

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

Cheers

Mike

Reply all
Reply to author
Forward
0 new messages