问题描述
我正在尝试在现有的Django模型中添加一个multiselect字段.我浏览了这三个SOF线程和其他一些博客,但没有找到所需的解决方案.每个人都在使用modelForms.
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.
我只希望在默认的django管理面板(与选择字段相同)中使用它.该字段将不会以前端/任何形式使用.纯粹出于管理目的.
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.
我如何实现这一目标.
我可以为此创建另一个模型,然后再创建多对多"关系,但是我想知道是否存在诸如此类的东西
I can create another model for this and then ManytoMany relation but I wonder if there existsome thing like
field = models.MultipleChoiceField(choices = [选项列表])
推荐答案
.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))
在您的表格中.py
from .models import SomeAwesomeModel
from django import forms
class SomeAwesomeModelForm(ModelForm):
class Meta:
model=SomeAawesomeModel
fields=['myfield']
这不会将您的字段放在前端,但是会在admin中显示为下拉列表.
This won't put your field in front-end but will show in admin as a drop-down.
这篇关于仅在Django管理面板中的多选下拉列表/列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!