开发者

Django: Is there any way use a multiple select for many-to-many field with an intermediary model?

The docs say:

When you specify an intermediary model using the through argument to开发者_如何学C a ManyToManyField, the admin will not display a widget by default.

OK, but how about if I want a multiple select widget?

I have a model:

class Quotation(models.Model):
 source    = models.CharField()
 sourceLink  = models.URLField( blank=True)
 text   = models.TextField()
 site   = models.ManyToManyField(Site, through="QuoteSite" )

and an intermediary model:

class QuoteSite(models.Model):
 entry    = models.ForeignKey(Quotation)
 site   = models.ForeignKey(Site)
 dateLastUsed = models.DateField(default=date(2000,01,01))

All I want to do is allow users in admin to select one or more sites for their quotation. I don't care whether they can edit the datelastUsed field in the intermediary model.

Is this impossible?


I solved my problem by defining the 'site' m2m field without the 'through', instead specifying the same db_table as used by the QuoteSite class. Because I retrieve my 'TodaysQuote()' using a manager on the QuoteSite class, not the Quotation class, it turns out there's no reason for the Quotation class to know about the dateLastUsed at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜