Ale*_*der 8 python forms csv django multiple-select
我有一个Django应用程序,并希望在Django的管理界面中显示多个选项复选框.我不想通过使用ManyToManyField为我的选择创建单独的模型.
models.py
from django.db import models
STAFF_BUSINESS_TYPES = {
(1, "Foo"),
(2, "Bar"),
(3, "Cat"),
(4, "Dog")
}
class Business(models.Model):
name = models.CharField(max_length=255, unique=True)
business_types = models.CommaSeparatedIntegerField(max_length=32, choices=STAFF_BUSINESS_TYPES)
Run Code Online (Sandbox Code Playgroud)
forms.py
from business.models import Business, STAFF_BUSINESS_TYPES
from django.forms import CheckboxSelectMultiple, ModelForm, MultipleChoiceField
class BusinessForm(ModelForm):
business_types = MultipleChoiceField(required=True, widget=CheckboxSelectMultiple, choices=STAFF_BUSINESS_TYPES)
class Meta:
model = Business
fields = ['name', 'business_types']
def clean_business_types(self):
data = self.cleaned_data['business_types']
cleaned_data = ",".join(data)
return cleaned_data
Run Code Online (Sandbox Code Playgroud)
admin.py
from django.contrib import admin
from business.models import Business
from business.forms import BusinessForm
@admin.register(Business)
class BusinessAdmin(admin.ModelAdmin):
form = BusinessForm
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试添加类型为"Bar"的商家时:
选择一个有效的选择.1不是可用的选择之一.
同样,当我尝试添加一个选择了多个值的业务时:
选择一个有效的选择.1,2不是可用的选择之一.
考虑到(1,"Foo")是否在我的选择范围内,1如何不是一个有效的选择?使用Django内置的逗号分隔整数字段是否无效?
我曾经遇到过类似的问题,这是我的解决方案:
# coding: utf-8
# python2 / django1.6.5
"""
That's a first solution I got to use CommaSeparatedIntegerFields with SelectMultiple widget.
My intension is to change this solution to a custom Widget that inherits from SelectMultiple.
*** It still needs refactoring ***
"""
Run Code Online (Sandbox Code Playgroud)
from django.db import models
MY_CHOICES = (
(1, 'escolha1'),
(2, 'escolha2'),
(3, 'escolha3'),
(4, 'escolha4'),
(5, 'escolha5'),
)
class MeuConteudo(models.Model):
meu_campo = models.CommaSeparatedIntegerField(
blank=True, max_length=255,
)
Run Code Online (Sandbox Code Playgroud)
from django import forms
from minhaapp.models import MeuConteudo, MY_CHOICES
class CommaSeparatedSelectInteger(forms.MultipleChoiceField):
def to_python(self, value):
if not value:
return ''
elif not isinstance(value, (list, tuple)):
raise ValidationError(
self.error_messages['invalid_list'], code='invalid_list'
)
return ','.join([str(val) for val in value])
def validate(self, value):
"""
Validates that the input is a string of integers separeted by comma.
"""
if self.required and not value:
raise ValidationError(
self.error_messages['required'], code='required'
)
# Validate that each value in the value list is in self.choices.
for val in value.split(','):
if not self.valid_value(val):
raise ValidationError(
self.error_messages['invalid_choice'],
code='invalid_choice',
params={'value': val},
)
def prepare_value(self, value):
""" Convert the string of comma separated integers in list"""
return value.split(',')
class MeuConteudoMultipleForm(forms.ModelForm):
meu_campo = CommaSeparatedSelectInteger(
choices=MY_CHOICES,
widget=forms.SelectMultiple
)
class Meta:
model = MeuConteudo
Run Code Online (Sandbox Code Playgroud)
from forms import MeuConteudoMultipleForm
from minhaapp.models import MeuConteudo
from minhaapp.forms import MeuConteudoMultipleForm
class MeuConteudoAdmin(admin.ModelAdmin):
form = MeuConteudoMultipleForm
admin.site.register(MeuConteudo, MeuConteudoMultipleForm)
https://gist.github.com/romulocollopy/bffe38fa72af5bc427e1
Run Code Online (Sandbox Code Playgroud)