Django - 更改内联formset textInput size属性 [英] Django - Changing inline formset textInput size attribute

查看:1075
本文介绍了Django - 更改内联formset textInput size属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内联表单,只有三个字段:

  class Estimate_Product_Details(models.Model):
proposalID = models.ForeignKey(Estimate_Construction,verbose_name ='Proposal ID')
CID = models.ForeignKey(Product,verbose_name ='CID')
qty = models.DecimalField(max_digits = 7,decimal_places = 2 ,verbose_name ='Quantity')

def __unicode __(self):
return u'%s - %s'%(self.proposalID,self.CID)

然后我从该模型中创建一个表单:

  class Product_Form(ModelForm):
class Meta:
model = Estimate_Product_Details
fields =('CID','qty')
widgets = {
'qty':forms.TextInput(attrs = {'size':30})
}

我的目标是让 qty 输入字段很小(我有30个进行测试)。但是,当我通过内联表单集渲染此表单时,该属性根本不设置。以下是我的视图中的formset的创建:

  pFormSet = inlineformset_factory(Estimate_Construction,Estimate_Product_Details,form = Product_Form)

我在哪里错了?为什么 qty 字段的大小会改变?

解决方案

编辑:它可能只是将一个类设置为输入字段,并让CSS分配宽度。这样你可以将它传递给不同客户端的模板。



可能不是使用 .attrs ['size'] = 30 使用 .attrs ['class'] ='some_class'然后在HTML模板中定义类,并处理不同的大小。



这应该工作:

  class Product_Form(ModelForm):
def __init __(self,* args,** kwargs):
super(Product_Form,self).__ init __(* args,** kwargs)
self.fields ['qty']。widget.attrs [' size'] = 30

class Meta:
model = Estimate_Product_Details
fields =('CID','qty')
/ pre>

I have an inline formset that only has three fields:

class Estimate_Product_Details(models.Model):
    proposalID = models.ForeignKey(Estimate_Construction, verbose_name='Proposal ID')
    CID = models.ForeignKey(Product, verbose_name = 'CID')
    qty = models.DecimalField(max_digits = 7, decimal_places = 2, verbose_name = 'Quantity')

    def __unicode__(self):
        return u'%s -- %s' % (self.proposalID, self.CID)

I then create a form from that model:

class Product_Form(ModelForm):
    class Meta:
        model = Estimate_Product_Details
        fields = ('CID', 'qty')
        widgets = {
            'qty' : forms.TextInput(attrs={'size':30})
            }

My goal is to have the qty input field really small (I have 30 there for testing). However, when I render this form via an inline formset, the attribute is not being set at all. Here's the creation of the formset in my view:

    pFormSet = inlineformset_factory(Estimate_Construction, Estimate_Product_Details, form = Product_Form)

Where am I going wrong? Why doesn't the qty field change in size?

解决方案

EDIT: It should probably just set a class to the input field and have the CSS assign the width. That way you could pass it to separate templates and such for different clients.

Perhaps instead of using .attrs['size'] = 30 use .attrs['class'] = 'some_class' then define the class in your HTML template and handle the different sizes there.

This should work:

class Product_Form(ModelForm):
    def __init__(self, *args, **kwargs):
        super(Product_Form, self).__init__(*args, **kwargs)
        self.fields['qty'].widget.attrs['size'] = 30

    class Meta:
        model = Estimate_Product_Details
        fields = ('CID', 'qty')

这篇关于Django - 更改内联formset textInput size属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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