在 django admin 中,我们可以根据选择进行多项选择吗

发布于 2024-08-10 12:59:53 字数 352 浏览 4 评论 0原文

http://docs.djangoproject.com/en/dev/ref/ models/fields/#choices

我已通读文档,这意味着使用数据库表来存储动态数据,但它指出

选择适用于不会发生太大变化(如果有的话)的静态数据。

那么,如果我想使用选择,但让它选择多个,因为我使用的数据是相当静态的,例如一周中的几天。

有没有办法在没有数据库表的情况下实现这一点?

http://docs.djangoproject.com/en/dev/ref/models/fields/#choices

i've read through the documentation and this implies using a database table for dynamic data, however it states

choices is meant for static data that doesn't change much, if ever.

so what if i want to use choices, but have it select multiple because the data i'm using is quite static, e.g days of the week.

is there anyway to achieve this without a database table?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-08-17 12:59:53

ChoiceField 并不真正适合多种选择,而是我会使用 多对多字段。暂时忽略静态数据可以使用 Choices 代替foreignkey 的事实。如果事实证明这是一个性能问题,有多种方法可以以不同的方式表示它(一种是二进制掩码方法),但它们需要更多的工作。

ChoiceField is not really suitable for multiple choices, instead I would use a ManyToManyField. Ignore the fact that Choices can be used instead of ForeignKey for static data for now. If it turns out to be a performance issue, there are ways to represent this differently (one being a binary mask approach), but they require way more work.

仙气飘飘 2024-08-17 12:59:53

这对我有用:

1)创建一个 Form 类并设置一个属性以向 MultipleChoiceField 提供静态选择

从 django 导入表单
从 myapp.models 导入 MyModel、MYCHOICES

类 MyForm(forms.ModelForm):
    myfield = forms.MultipleChoiceField(choices=MYCHOICES, widget=forms.SelectMultiple)
    类元:
        模型=我的模型

2)然后,如果您使用的是管理界面,请在管理类中设置表单属性,以便tit将使用您的自定义表单

从 myapp.models 导入 MyModel
从 myapp.forms 导入 MyForm
从 django.contrib 导入 admin

类 MyAdmin(admin.ModelAdmin):
    表格=我的表格

admin.site.register(MyModel, MyAdmin)

This worked for me:

1) create a Form class and set an attribute to provide your static choices to a MultipleChoiceField

from django import forms
from myapp.models import MyModel, MYCHOICES

class MyForm(forms.ModelForm):
    myfield = forms.MultipleChoiceField(choices=MYCHOICES, widget=forms.SelectMultiple)
    class Meta:
        model = MyModel

2) then, if you're using the admin interface, set the form attribute in your admin class so tit will use your customized form

from myapp.models import MyModel
from myapp.forms import MyForm
from django.contrib import admin

class MyAdmin(admin.ModelAdmin):
    form = MyForm

admin.site.register(MyModel, MyAdmin)
南街女流氓 2024-08-17 12:59:53

尝试以下配置。在models.py

class MyModel(models.Model):
    my_choices = models.TextField(help_text="It's a good manners to write it")

中,在forms.py中,

CHOICES = ((1,1), (2,2))
class MyForm(forms.ModelForm):
    my_choices = forms.MultipleChoiceField(choices=CHOICES)

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        # maybe you can set initial with self.fields['my_choices'].initial = initial 
        # but it doesn't work wity dynamic choices
        obj = kwargs.get('instance')
        if obj:
            initial = [i for i in obj.my_choices.split(',')]
            self.initial['my_choices'] = initial

    def clean_lead_fields(self):
        return ','.join(self.cleaned_data.get('my_choices', []))

admin.py

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm

Try following configuration. In models.py

class MyModel(models.Model):
    my_choices = models.TextField(help_text="It's a good manners to write it")

in forms.py

CHOICES = ((1,1), (2,2))
class MyForm(forms.ModelForm):
    my_choices = forms.MultipleChoiceField(choices=CHOICES)

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        # maybe you can set initial with self.fields['my_choices'].initial = initial 
        # but it doesn't work wity dynamic choices
        obj = kwargs.get('instance')
        if obj:
            initial = [i for i in obj.my_choices.split(',')]
            self.initial['my_choices'] = initial

    def clean_lead_fields(self):
        return ','.join(self.cleaned_data.get('my_choices', []))

in admin.py

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    form = MyModelForm
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文