django模型选择选项作为多选框 [英] django model choice option as a multi select box

查看:208
本文介绍了django模型选择选项作为多选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的模型

  COLORS =(
('R','Red'),
('B','黄'),
('G','白'),

class Car(models.Model):
name = models.CharField(max_length = 20)
color = models.CharField(max_length = 1,choices = COLORS)

它在管理面板中显示为一个选择框,但是我希望我的管理员用户多选择这些颜色,如多对多关系,如果没有 'RB','Red& Blue'),逻辑类型

解决方案

可以 Car 有多个颜色 s?在这种情况下, color 应该是一个多对多关系,而不是一个 CharField 。另一方面,如果你想做一些像 Unix权限(ie红色+蓝色,红色+蓝色+绿色等)然后为每个颜色分配数值,并使颜色 an 整数字段



更新



(阅读评论后)您可以使用自定义表单来编辑您的模型管理员而不是默认的 ModelForm 。此自定义表单可以使用多选择小部件,允许用户选择多种颜色。然后,您可以覆盖表单的 clean()方法,以返回适当的级联值(RB等)。



更新2



以下是一些代码:



从模型字段中删除选项。还可以将其最大尺寸增加到2.我们不希望在这里进行选择 - 如果我们这样做,那么我们必须为每种颜色组合添加一个选择。

  class Car(models.Model):
...
color = models.CharField(max_length = 2)

第二个添加自定义 ModelForm 在admin应用程序中使用此表单将覆盖颜色,而将其声明为多项选择字段。

  COLORS =(
('R','红色'),
('B','黄色'),
('G','白'),


class CarAdminForm(ModelForm) :
color = forms.MultipleChoiceField(choices = COLORS)

class Meta:
model = Car

def clean_color(self):
color = self.cleaned_data ['color']
如果不是颜色:
raise forms.ValidationError(...)

如果len(color)> 2:
raise forms.ValidationError(...)

color =''.join(color)
return color

请注意,我只添加了几个验证。您可能需要更多和/或自定义验证。



最后,请使用管理员注册此表单。在$ admin.py 中:

  class CarAdmin(admin.ModelAdmin) :
form = CarAdminForm

admin.site.register(Car,CarAdmin)


Assuming that I have such model

COLORS= (
    ('R', 'Red'),
    ('B', 'Yellow'),
    ('G', 'White'),
)
class Car(models.Model):
    name = models.CharField(max_length=20)
    color= models.CharField(max_length=1, choices=COLORS)

It displays as a selectbox in the admin panel however I would like my admin-user to multi select those colors like many-to-many relationship, how can this be achieved without a ('RB', 'Red&Blue'), type of logic

解决方案

Can a Car have multiple colors? In that case color ought to be a many to many relationship rather than a CharField. If on the other hand you want to do something like Unix permissions (i.e. Red + Blue, Red + Blue + Green etc.) then assign numeric values of to each of them and make color an integer field.

Update

(After reading comment) You can use a custom form to edit your model in Admin instead of the default ModelForm. This custom form can use a multiple choice widget that lets users select multiple colors. You can then override the clean() method of the form to return a suitably concatenated value ('RB' etc.).

Update 2

Here is some code:

First, remove the choices from the model field. Also increase its maximum size to 2. We don't want choices here - if we do, then we'll have to add a choice for each combination of colors.

class Car(models.Model):
    ...
    color= models.CharField(max_length=2)

Second add a custom ModelForm to use in admin app. This form will override color and instead declare it as a multiple choice field. We do need choices here.

COLORS= (
    ('R', 'Red'),
    ('B', 'Yellow'),
    ('G', 'White'),
)

class CarAdminForm(ModelForm):
    color = forms.MultipleChoiceField(choices = COLORS)

    class Meta:
        model = Car

    def clean_color(self):
        color = self.cleaned_data['color']
        if not color:
            raise forms.ValidationError("...")

        if len(color) > 2:
            raise forms.ValidationError("...")

        color = ''.join(color)
        return color

Note that I have added only a couple of validations. You may want more and/or customize the validations.

Finally, register this form with admin. Inside your admin.py:

class CarAdmin(admin.ModelAdmin):
    form = CarAdminForm

admin.site.register(Car, CarAdmin)

这篇关于django模型选择选项作为多选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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