Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 462
Nancy Avatar asked Jun 12 '26 07:06

Nancy


1 Answers

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.

like image 75
abhi Avatar answered Jun 13 '26 23:06

abhi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!