Multiselect dropdown/list in django admin panel only

Deadly 提交于 2021-01-27 12:41:26

问题


django model choice option as a multi select box

Django Multiple Choice Field / Checkbox Select Multiple

Django: How can i create a multiple select form?

I'm trying to add a multiselect field in existing django model. I went through these three SOF threads and some other blogs but I didn't find required solution. everyone is using modelForms.

I want this in purely in default django admin panel (same as choice field). this field is not going to be used in front-end/any kind of form. purely for admin purpose.

How can I achieve this.

I can create another model for this and then ManytoMany relation but I wonder if there exist some thing like

field = models.MultipleChoiceField(choices=[a list of choices])


回答1:


in your models.py

from django.db import models
from model_utils import Choices
awesome_choices=('Choice 1', 'Choice 2',)
class SomeAwesomeModel(models.Model):
    myfield=models.CharField(max_length=255)
    field_not_in_front_end=models.Charfield(max_length=255, choices=Choices(*awesome_choices))

in your forms.py

from .models import SomeAwesomeModel
from django import forms

class SomeAwesomeModelForm(ModelForm):
    class Meta:
        model=SomeAawesomeModel
        fields=['myfield']

This won't put your field in front-end but will show in admin as a drop-down.




回答2:


If you want to be able to select multiple values at the same time, use this project's MultiSelectField:

https://github.com/goinnn/django-multiselectfield

MultiSelectField renders a group of independently-selectable checkboxes in the Django admin page.



来源:https://stackoverflow.com/questions/25382357/multiselect-dropdown-list-in-django-admin-panel-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!