django formset中的动态模型选择字段使用多个选择元素 [英] Dynamic model choice field in django formset using multiple select elements

查看:97
本文介绍了django formset中的动态模型选择字段使用多个选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在django用户列表中发布了这个问题,但还没有回复。



我的模型看起来像这样: p>

  class ProductGroup(models.Model):
name = models.CharField(max_length = 10,primary_key = True)
def __unicode __(self):return self.name

class ProductRun(models.Model):
date = models.DateField(primary_key = True)
def __unicode __ ):return self.date.isoformat()

class CatalogItem(models.Model):
cid = models.CharField(max_length = 25,primary_key = True)
group = models.ForeignKey(ProductGroup)
run = models.ForeignKey(ProductRun)
pnumber = models.IntegerField()
def __unicode __(self):return self.cid
class Meta:
unique_together =('group','run','pnumber')

class Transaction(models.Model):
timestamp = models.DateTimeField()
用户= models.ForeignKey(User)
item = models.ForeignKey(CatalogItem)
quantity = models.IntegerField()
price = models.FloatField()
ProductRun。每组有20-200个不同的
产品编号(pnumber),所以至少有几千
CatalogItems。



我在工作事务模型的formets。而不是
单选菜单,其中包含
ForeignKey字段的几千个CatalogItem,我想替换三个下拉菜单,对于
group,run和pnumber,它们唯一标识CatalogItem。
我也想将下两个下拉菜单中的选项限制为
那些可用于当前
所选产品组的运行和pnumbers(我可以通过AJAX更新它们用户
更改了产品组,但重要的是初始页
加载描述,而不依赖于AJAX)。



最好的方法是什么为了做到这一点?



作为出发点,这里是我迄今为止所尝试/考虑的:



我的第一种方法是从
表单中排除项目外键字段,通过覆盖formset的add_fields
方法来添加替代下拉列表,然后提取数据并填充
字段在模型实例上手动保存之前。这是
直截了当,很简单,但它不是很可重用,我
不认为这是正确的方法。



我的第二种方法是创建一个继承
MultiValueField和ModelChoiceField的新字段,以及相应的
MultiWidget子类。这似乎是正确的做法。作为
Malcolm Tredinnick将其放在
django-users讨论
我们遇到的问题是何时/哪里可以获取选项列表
从一个字段的聪明位于Field类中。



数据库。我现在在该领域的 __ init __
中的代码,但这意味着我必须知道在处理
之前的哪个ProductGroup甚至定义Form类,因为我必须在定义表单时实例化
Field。所以我有一个工厂
功能,我在最后一分钟从我的看法 - 我知道
我有什么CatalogItem和他们在哪个产品组 - 到
创建表单/ formset类并实例化它们。它的工作,但我
想知道是否有更好的方法。毕竟,该字段应该是
,以便稍后确定正确的选择,一旦它知道
它的当前值。



另一个问题是我的实现将整个formset
限制在与单个
ProductGroup相关的事务(CatalogItems)。



我有趣的第三种可能性是把它全部放在Widget
类中。一旦我有相关的模型实例,或者cid,或者
,无论面板是什么,我可以得到ProductGroup和
构造下拉列表。这将解决我的
第二种方法的问题,但似乎不是正确的方法。

解决方案

我最后坚持第二种方法,但我现在相信这是短路是很长的。我不得不在ModelForm和FormField内部挖掘一下,而IMO的复杂性超过了最小的收益。



我在第一个方法的问题中写道:这很简单,很简单,应该是小费了。


I posted this question on the django-users list, but haven't had a reply there yet.

I have models that look something like this:

class ProductGroup(models.Model):
     name = models.CharField(max_length=10, primary_key=True)
     def __unicode__(self): return self.name

class ProductRun(models.Model):
     date = models.DateField(primary_key=True)
     def __unicode__(self): return self.date.isoformat()

class CatalogItem(models.Model):
     cid     = models.CharField(max_length=25, primary_key=True)
     group   = models.ForeignKey(ProductGroup)
     run     = models.ForeignKey(ProductRun)
     pnumber = models.IntegerField()
     def __unicode__(self): return self.cid
     class Meta:
         unique_together = ('group', 'run', 'pnumber')

class Transaction(models.Model):
     timestamp   = models.DateTimeField()
     user        = models.ForeignKey(User)
     item        = models.ForeignKey(CatalogItem)
     quantity    = models.IntegerField()
     price       = models.FloatField()

Let's say there are about 10 ProductGroups and 10-20 relevant ProductRuns at any given time. Each group has 20-200 distinct product numbers (pnumber), so there are at least a few thousand CatalogItems.

I am working on formsets for the Transaction model. Instead of a single select menu with the several thousand CatalogItems for the ForeignKey field, I want to substitute three drop-down menus, for group, run, and pnumber, which uniquely identify the CatalogItem. I'd also like to limit the choices in the second two drop-downs to those runs and pnumbers which are available for the currently selected product group (I can update them via AJAX if the user changes the product group, but it's important that the initial page load as described without relying on AJAX).

What's the best way to do this?

As a point of departure, here's what I've tried/considered so far:

My first approach was to exclude the item foreign key field from the form, add the substitute dropdowns by overriding the add_fields method of the formset, and then extract the data and populate the fields manually on the model instances before saving them. It's straightforward and pretty simple, but it's not very reusable and I don't think it is the right way to do this.

My second approach was to create a new field which inherits both MultiValueField and ModelChoiceField, and a corresponding MultiWidget subclass. This seems like the right approach. As Malcolm Tredinnick put it in a django-users discussion, "the 'smarts' of a field lie in the Field class."

The problem I'm having is when/where to fetch the lists of choices from the db. The code I have now does it in the Field's __init__, but that means I have to know which ProductGroup I'm dealing with before I can even define the Form class, since I have to instantiate the Field when I define the form. So I have a factory function which I call at the last minute from my view--after I know what CatalogItems I have and which product group they're in--to create form/formset classes and instantiate them. It works, but I wonder if there's a better way. After all, the field should be able to determine the correct choices much later on, once it knows its current value.

Another problem is that my implementation limits the entire formset to transactions relating to (CatalogItems from) a single ProductGroup.

A third possibility I'm entertaining is to put it all in the Widget class. Once I have the related model instance, or the cid, or whatever the widget is given, I can get the ProductGroup and construct the drop-downs. This would solve the issues with my second approach, but doesn't seem like the right approach.

解决方案

I ended up sticking with the second approach, but I'm convinced now that it was the Short Way That Was Very Long. I had to dig around a bit in the ModelForm and FormField innards, and IMO the complexity outweighs the minimal benefits.

What I wrote in the question about the first approach, "It's straightforward and pretty simple," should have been the tip-off.

这篇关于django formset中的动态模型选择字段使用多个选择元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆