I have a typical M2M scenario where promotion activities are related to our retailers. However we have a large number of retailers (over 10k) and therefore I can't use the normal multiple select widget.
我有一个典型的M2M场景,促销活动与我们的零售商有关。但是我们有大量零售商(超过10k),因此我无法使用普通的多选小部件。
What I would aim to do is have an 'activity' instance page with a 'retailer' sub-page which would have a table listing all those retailers currently related to the activity. In addition there would be a 'delete' checkbox next to each retailer so they could be removed from the list if necessary. (Naturally, I would also have another search/results page where users could select which retailers they want to add to the list, but I'm sure I can sort that out myself).
我打算做的是拥有一个“活动”实例页面,其中包含一个“零售商”子页面,该子页面上有一个表格,列出了当前与该活动相关的所有零售商。此外,每个零售商旁边都会有一个“删除”复选框,因此如有必要,可以从列表中删除它们。 (当然,我还有另一个搜索/结果页面,用户可以选择他们想要添加到列表中的零售商,但我相信我可以自己解决这个问题)。
Could someone point me in the right direction regarding modelforms and formset factories as I'm not sure where to go from here. It would seem obvious to directly manipulate the app_activity_associated_retailers table but I don't think I can do this with the existing functions. Is there was a pattern for doing this.
有人能指出我关于模型和模板工厂的正确方向,因为我不知道从哪里开始。直接操作app_activity_associated_retailers表似乎很明显,但我不认为我可以使用现有函数执行此操作。是否存在这样做的模式。
class Activity(models.Model):
budget = models.ForeignKey('Budget')
activity_type = models.ForeignKey('ActivityType')
amount = models.DecimalField(max_digits=8, decimal_places=2)
associated_retailers = models.ManyToManyField('Retailer', related_name='associated_retailers')
class Retailer(models.Model):
name = models.CharField(max_length=50)
address01 = models.CharField(max_length=50)
address02 = models.CharField(max_length=50, blank=True)
postcode = models.CharField(max_length=5)
city = models.CharField(max_length=20)
1 个解决方案
#1
6
All ManyToManyField
s have a through
model, whether you define one yourself or not. In your case, it'll have an id
, an activity
field and a retailer
field. You can access the table with Activity.associated_retailers.through
-- one "obvious" way is to just expose it as a "model" like
所有ManyToManyField都有一个直通模型,无论你是否自己定义。在您的情况下,它将有一个id,一个活动字段和一个零售商字段。您可以使用Activity.associated_retailers.through访问该表 - 一种“明显”的方式是将其公开为“模型”,如
ActivityRetailer = Activity.associated_retailers.through
You can now manipulate these relationships like they were any ol' Django model, so you can i.e. generate querysets like
你现在可以操纵这些关系,就像它们是任何ol'Django模型一样,所以你可以生成像这样的查询集
retailer_records_for_activity = ActivityRetailer.objects.filter(activity_id=1234)
... and you can also create model formsets (complete with that delete
checkbox if so configured) for these pseudo-models.
...并且您还可以为这些伪模型创建模型表单集(如果这样配置,则使用该删除复选框完成)。
#1
6
All ManyToManyField
s have a through
model, whether you define one yourself or not. In your case, it'll have an id
, an activity
field and a retailer
field. You can access the table with Activity.associated_retailers.through
-- one "obvious" way is to just expose it as a "model" like
所有ManyToManyField都有一个直通模型,无论你是否自己定义。在您的情况下,它将有一个id,一个活动字段和一个零售商字段。您可以使用Activity.associated_retailers.through访问该表 - 一种“明显”的方式是将其公开为“模型”,如
ActivityRetailer = Activity.associated_retailers.through
You can now manipulate these relationships like they were any ol' Django model, so you can i.e. generate querysets like
你现在可以操纵这些关系,就像它们是任何ol'Django模型一样,所以你可以生成像这样的查询集
retailer_records_for_activity = ActivityRetailer.objects.filter(activity_id=1234)
... and you can also create model formsets (complete with that delete
checkbox if so configured) for these pseudo-models.
...并且您还可以为这些伪模型创建模型表单集(如果这样配置,则使用该删除复选框完成)。