如何将一个参数传递给表单中的表单? [英] How to pass in a parameter to the form within the formset?

查看:140
本文介绍了如何将一个参数传递给表单中的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个表单,每个表单都有一个下拉列表,指向一组销售项目。



模型: / p>

  class SalesItem(models.Model):
item_description = models.CharField(max_length = 40)
公司= models.ForeignKey(公司)

这里我创建一个带有下拉菜单的表单,希望通过公司作为下拉菜单的来源。坚持这个想法,因为我认为在我的情况下是不可能的。



形式:

  class SalesItemFSForm(Form):
sales_item = forms.ModelChoiceField(required = False,queryset ='')

def __init__ (self,company,* args,** kwargs):
super(SalesItemFSForm,self).__ init __(* args,** kwargs)
self.fields.sales_item.queryset = company.salesitem_set.all ()

现在在我看来,我想用这种形式创建一个表单:

  formset_type = formset_factory(SalesItemFSForm,extra = 0)

问题立即清楚,因为我似乎无法通过公司确定下拉菜单的来源。



非常感谢,



更新: / strong>



似乎Jingo破解了它。 :)



一个ModelForm比Form更好。除此之外,我必须向SalesItemFSForm添加 fields = {} ,以确保SalesItem的字段未显示在模板中。因为我们感兴趣的是我们的下拉菜单(SalesItem)。



到目前为止这么好。但是现在我看到有许多下拉列表显示为我有Salesitems。除非用户按下jquery按钮,否则它不会显示任何内容。



我认为这是问题,我们不应该传入

  formset_type = modelformset_factory(SalesItem,form = SalesItemFSForm,extra = 0)

不需要SalesItem的任何实例。我们需要一个虚拟模型。



这是我最初用经典的Formset而不是ModelFormset来解决它的原因。所以它的那种半途而废。 :)



更新2:



Jingo,好点。有效地,我正在考虑一个自定义保存,其中我只是看到用户通过jQuery添加了多少个表单,并将其自己保存在视图中。字面上SalesItem是一个ManyToMany字段。但标准的M2m小部件是可怕的。因此,我想用formets替换它,其中每个salesItem都是一个下拉列表。然后,用户可以添加尽可能多的下拉列表(formset中的表单)到页面并提交。然后我会在视图中添加关系。

  class DealType(models.Model):
deal_name = models。 CharField(_(u交易名称),max_length = 40)
sales_item = models.ManyToManyField(SalesItem)
price = models.DecimalField(decimal_places = 2,max_digits = 12)

希望这一点清楚。也许有一个更简单的方法来做到这一点。 :)



Btw我还发现这个优秀的jquery 代码段代码如何向/从表单集添加/删除表单。



更新3:



事实上,如实例化这样的对象,只能在表单集中获得一个表单,并且可以通过jquery添加更多表单。完善!!除非有一个更简单的方法来实现这一点。 :

  salesitem_formsets = formset_type(queryset = SalesItem.objects.filter(pk = 1))

但是,这个请求中的狩猎您的POST,因为您不能只做:

  salesitem_formsets = formset_type(request.POST)

仍然需要设置queryset。

解决方案

我希望我理解你想要实现的目标。那么也许你可以使用ModelForm及其可用的实例:

  class SalesItemFSForm(forms.ModelForm):
class Meta:
model = SalesItem

def __init __(self,* args,** kwargs):
super(SalesItemFSForm,self).__ init __(* args,** kwargs)
self.sale_items = self.instance.company.salesitem_set.all()
self.fields ['sales_item'] = forms.ModelChoiceField(queryset = self.sale_items)

这是未经测试的,只是一个想法。我希望这会导致正确的方向,但如果它完全错了,让我知道,我会删除我的答案,让别人不要混淆:)。


I would like to create a formset, where each form has a dropdown pointing to a set of sales items.

Model:

class SalesItem(models.Model):        
    item_description    = models.CharField(max_length=40)
    company             = models.ForeignKey(Company)

Here I create a form with a dropdown, hoping to pass in the company as a source for the dropdown. Hold on to this thought, as I think that is not possible in my scenario.

Form:

class SalesItemFSForm(Form):        
    sales_item      =   forms.ModelChoiceField(required=False, queryset = '')

    def __init__(self, company, *args, **kwargs):
        super(SalesItemFSForm, self).__init__(*args, **kwargs)
        self.fields.sales_item.queryset = company.salesitem_set.all()

Now within my view I would like to create a formset with this form:

formset_type = formset_factory(SalesItemFSForm, extra=0)

The problem becomes right away clear, as there seem to be no way that I could pass in the company to determine the source for the dropdown.

How am I supposed to do this?

Many Thanks,

Update:

it seems Jingo cracked it. :)

A ModelForm works better than a Form. On top of it I had to add fields = {} to SalesItemFSForm, to make sure that the SalesItem's fields are not showing up in the template. Because all we are interested in is our dropdown (SalesItem).

So far so good. But now I see as many dropdowns shown as I have Salesitems. It shouldn;t show any unless the user presses a jquery button.

And I think this is the problem, we should NOT pass in

formset_type = modelformset_factory(SalesItem, form=SalesItemFSForm, extra=0)

Because our form doesn't need any instance of the SalesItem. We need a dummy Model.

That was the reason I tried to solve it initially with classic Formset instead of ModelFormset. So its kind of half way there. :)

Update 2:

Jingo, good point. Effectively I was thinking of a custom save, where I just see how many formsets are added by the user via jQuery and save it myself within the view. Literally SalesItem is a ManyToMany field. But the standard M2m widget is horrible. Hence I wanted to replace it with formsets, where each salesItem is a dropdown. The user can then add as many dropdowns (forms in formset) to the page and submit them. Then I would add the relationship in the view.

class DealType(models.Model):    
    deal_name           = models.CharField(_(u"Deal Name"), max_length=40)
    sales_item          = models.ManyToManyField(SalesItem)    
    price               = models.DecimalField(decimal_places=2, max_digits=12)

Hope this makes it clear. Maybe there is an easier way to do this. :)

Btw I also found this excellent jquery snippet code how to add/remove forms to/from a formset.

Update 3:

Indeed when instantiating the object like this, we would only get one form in the formset and can add more via jquery. Perfect!! Unless there is an easier way to achieve this. :)

salesitem_formsets = formset_type(queryset=SalesItem.objects.filter(pk=1))

However this comes back hunting you in the request.POST, since you can't just do:

salesitem_formsets = formset_type(request.POST)

It still requires the queryset to be set. Tricky situation...

解决方案

I hope I understood the goal you want to achieve right. Then maybe you could use ModelForm and its available instance like this:

class SalesItemFSForm(forms.ModelForm):
    class Meta:
        model = SalesItem

    def __init__(self, *args, **kwargs):
        super(SalesItemFSForm, self).__init__(*args, **kwargs)
        self.sale_items = self.instance.company.salesitem_set.all()
        self.fields['sales_item'] = forms.ModelChoiceField(queryset=self.sale_items)

This is untested though and just a thought. I hope this leads into the right direction, but if its totally wrong, let me know and i will remove my answer, so that others wont be confused :).

这篇关于如何将一个参数传递给表单中的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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