简体   繁体   中英

Multiselect dropdown/list in django admin panel only

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])

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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