Django - 将窗体集中的BooleanField显示为一组单选按钮 [英] Django - Show BooleanField in a formset as one group of radio buttons

查看:678
本文介绍了Django - 将窗体集中的BooleanField显示为一组单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

class Profile(models.Model):
    verified = models.BooleanField(default=False)

    def primary_phone(self):
        return self.phone_set.get(primary=True)

class Phone(models.Model):
    profile = models.ForeignKey(Profile)
    type = models.CharField(choices=PHONE_TYPES, max_length=16)
    number = models.CharField(max_length=32)
    primary = models.BooleanField(default=False)

    def save(self, force_insert=False, force_update=False, using=None):
        if self.primary:
            # clear the primary attribute of other phones of the related profile
            self.profile.phone_set.update(primary=False)
        self.save(force_insert, force_update, using)

我在ModelForm中使用 Phone 作为表单集。我想要做的是将 Phone.primary 显示为每个电话的实例旁边的单选按钮。如果我将主要作为 RadioSelect 小部件:

I'm using Phone in a ModelForm as a formset. What I'm trying to do is to show Phone.primary as a radio button beside each instance of Phone. If I make primary as a RadioSelect widget:

class PhoneForm(ModelForm):
    primary = forms.BooleanField(widget=forms.RadioSelect( choices=((0, 'False'), (1, 'True')) ))

    class Meta:
        from accounts.models import Phone
        model = Phone
        fields = ('primary', 'type', 'number', )

它将显示两个单选按钮,它们将在每个实例旁边分组。相反,我正在寻找一种方法来显示每个实例旁边的一个单选按钮(应该为该实例设置 primary = True ),并具有所有的单选按钮分组在一起,以便只能选择其中一个。

It will show two radio buttons, and they will be grouped together next to each instance. Instead, I'm looking for a way to show only one radio button next to each instance (which should set primary=True for that instance), and have all the set of radio buttons grouped together so that only one of them can be chosen.

我也在寻找一个干净的方式来做到这一点,我可以做大部分手动 - 在我的脑海里 - 但是我有兴趣看看是否有更好的方式来做,一种django式的方式。

I'm also looking for a clean way of doing this, I can do most of the above manually - in my head - but I'm interested to see if there is a better way to do it, a django-style way.

任何人都有想法? p>

Anyone got an idea?

推荐答案

确定你在这里有两个困境。首先,您需要通过给予相同的HTML名称属性来对来自不同表单的所有无线电选择进行分组。我使用下面的 add_prefix 覆盖。

Ok you've got two dilemma's here. First is you need to group all radio selects from different formsets by giving them the same HTML name attribute. I did that with the add_prefix override below.

然后你必须确保你的帖子数据中的主要字段返回一些有意义的内容,您可以从中确定选择了哪个手机(在POST数据b / c中只能有一个名称值,您只能从组中选择一个单选按钮)。通过分配正确的前缀值(这需要在 _ init _ 下完成,以便您可以访问自我实例),您可以将主值与其余的表单数据(通过自定义保存方法)。

Then you have to make sure that the 'primary' field in your post data returns something meaningful, from which you can determine which phone was selected (there should only be one 'name' value in POST data b/c you can only select one radio button from within a group). By assigning the proper prefix value (this needs to be done under _init_ so you can access the self instance), you'll be able to associate the 'primary' value with the rest of its form data (through a custom save method).

我使用以下内容测试了一个表单,并将其右键移出。所以尝试一下:

I tested a formset with the following and it spat out the right html. So give this a try:

class PhoneForm(ModelForm):
    def __init__ (self, *args, **kwargs)
        super(PerstransForm, self).__init__(*args, **kwargs)
        self.fields['primary'] = forms.BooleanField( widget = forms.RadioSelect(choices=((self.prefix, 'This is my Primary Phone'),))

    #enter your fields except primary as you had before.
    def add_prefix(self, field):
        if field == 'primary': return field
        else: return self.prefix and ('%s-%s' % (self.prefix, field)) or field

这篇关于Django - 将窗体集中的BooleanField显示为一组单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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