简体   繁体   English

在 django 中向管理员显示多个选项

[英]Show multiple choices to admin in django

I wanted to show multiple choices to admin so that at one time he can select more then one from these choices.我想向管理员展示多种选择,以便他一次可以从这些选择中选择 select 多于一个。 I can do this using check boxes fields.I have tried this but instead of showing check boxes it shows me drop down list of choices.我可以使用复选框字段来做到这一点。我已经尝试过了,但它没有显示复选框,而是显示了下拉选项列表。

Here is my code.这是我的代码。

models.py模型.py

class segmentation_Rules(models.Model):
        Segmentation_Rules_CHOICES = (
                        (1, 'At least one order'),
                        (2, 'Have reward points'),
                        )
        Rules       =models.CharField(max_length=100, blank=True,verbose_name="Select rules for customer segmentation",choices=Segmentation_Rules_CHOICES) 

forms.py forms.py

class Segmentation_Form(ModelForm):
        Rules = forms.MultipleChoiceField( widget=forms.CheckboxSelectMultiple)

admin.py管理员.py

class MyAdmin(admin.ModelAdmin):
    form = Segmentation_Form

So please show me some way so that admin can select multiple fields from choices.所以请告诉我一些方法,以便管理员可以选择 select 多个字段。

EDIT:编辑:

And if I remove the choices from models and define them into forms then there is just a text field shown to admin with no choices.如果我从模型中删除选择并将它们定义到 forms 中,那么只有一个文本字段显示给管理员,没有选择。

Segmentation_Rules_CHOICES = (
            (1, 'At least one order'),
            (2, 'Have reward points'),
            )

class Segmentation_Form(ModelForm):
        Rules = forms.MultipleChoiceField(choices=Segmentation_Rules_CHOICES, widget=forms.CheckboxSelectMultiple())

        class Meta:
            model=segmentation_Rules

You need to remove the choices argument from the model field definition in models.py and add choices field to the Rules form field in forms.py .您需要从models.py中的 model 字段定义中删除choices参数,并将choices字段添加到forms.pyRules表单字段中。 Like so:像这样:

models.py模型.py

class segmentation_Rules(models.Model):
    Segmentation_Rules_CHOICES = (
        (1, 'At least one order'),
        (2, 'Have reward points'),
    )
    Rules = models.CharField(max_length=100, blank=True, verbose_name="Select rules for customer segmentation") 

forms.py forms.py

class Segmentation_Form(ModelForm):
    Rules = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), choices=models.segmentation_Rules.Segmentation_Rules_CHOICES)

yes you can do that.是的,你可以这么做。 You need to use the MultipleChoiceField field with a CheckboxSelectMultiple widget to do that.您需要使用带有CheckboxSelectMultiple小部件的MultipleChoiceField字段来执行此操作。 I think you are doing it right but maybe you forgot () in widgets?我认为您做得对,但也许您在小部件中忘记了()

class Segmentation_Form(forms.Form):
    Rules = forms.MultipleChoiceField(choices= Segmentation_Rules_CHOICES, widget=forms.CheckboxSelectMultiple())

    def clean_Rules(self):
        if len(self.cleaned_data['Rules']) > 3:
            raise forms.ValidationError('Select no more than 3.')
        return self.cleaned_data['Rules']

I have thrown in a validation method.我已经抛出了一个验证方法。 Where you can have a limit on number of choices selected.您可以在其中限制所选选项的数量。

I am using this and its working fine我正在使用它并且它工作正常

Rules = forms.MultipleChoiceField(choices=mychoices, widget=forms.CheckboxSelectMultiple)

i think you don't need () at the send of CheckboxSelectMultiple我认为您在CheckboxSelectMultiple发送时不需要()

Use Multiple Choices in Admin with CharField Model使用 CharField Model 在管理员中使用多项选择

It stores the choices seperated by comma.它存储用逗号分隔的选项。

models.py and admin.py as they are models.pyadmin.py原样

forms.py forms.py

from my_project.model import segmentation_Rules 

class Segmentation_Form(ModelForm):
      Rules = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=segmentation_Rules.Segmentation_Rules_CHOICES, required=False)

      def __init__(self, *args, **kwargs):
          super(Segmentation_Form, self).__init__(*args, **kwargs)
          if kwargs.get('instance'):
              self.initial['Rules'] = eval(self.initial['Rules'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM