3👍
Solution:
Setting the choices by a integer, instead of a string.
POST_TYPES = (
(1, 'Blog'),
(2, 'Portfolio'),
(3, 'Beeldbank'),
)
Damn, that wasn’t worth breaking my skull over.
2👍
Might not be correct, but for my usecase, I did not want to replace the values with integers (as per the accepted answer). This was arrived at by a smidge of trial-and-error, and a lot of stepping through Django internals. Works for me, but YMMV:
from django.forms.widgets import (
CheckboxSelectMultiple as OriginalCheckboxSelectMultiple,
)
class CheckboxSelectMultiple(OriginalCheckboxSelectMultiple):
def optgroups(self, name, value, attrs=None):
# values come back as e.g. `['foo,bar']`, which we don't want when inferring "selected"
return super().optgroups(name, value[0].split(","), attrs)
- [Django]-How to load bootstrapped models of backbone in Django
- [Django]-NoReverseMatch: with arguments '()' and keyword arguments
0👍
Maybe I’m not understanding your question completely, but it seems like you could simplify a little. Using ModelForms, I don’t think any of your overriding the _init_ in your form is necessary. Try this and see if you get your desired behavior.
models.py
class Module(models.Model): POST_TYPES = ( ('blog', 'Blog'), ('portfolio', 'Portfolio'), ) title = models.CharField(max_length=100, verbose_name='title') entriesFrom = models.CharField(max_length=100, verbose_name='Pull content from', choices=POST_TYPES, blank=True) def __unicode__(self): return self.title
forms.py
class ModuleForm(forms.ModelForm): class Meta: model = Module
admin.py
from django.contrib import admin admin.site.register(models.Module)
If my answer isn’t what you’re looking for, try clarifying your question and I’ll see if I can help you out.
- [Django]-Upgrading from Django 1.4 to Django 1.7 – will it work?
- [Django]-Dynamically add properties to a django model
- [Django]-Django SelectDateWidget Years in reverse
- [Django]-Render Foreign Key in template as Checkboxes instead of Select in Django
0👍
you can use this function to remove string mark
from ast import literal_eval
literal_eval(value)
- [Django]-How to save foreign keys using django-rest-framework
- [Django]-Errors while installing python packages
0👍
I faced this issue, my changes haven’t affected on save.
I use CharField in model, but in forms.py;
class ModuleForm(forms.ModelForm):
my_field = forms.MultipleChoiceField(
choices=POST_TYPES,
widget=CheckboxSelectMultiple,
required=False,)
def __init__(self, *args, **kwargs):
super(ModuleForm, self).__init__(*args, **kwargs)
if kwargs.get('instance'):
self.initial['my_field'] = eval(self.initial['my_field'])
This form solution worked for me on MultipleChoiceField on Django Admin.
- [Django]-Django: Auto Generate a CharField using data from another CharField
- [Django]-POST on ModelViewSet with nested object Django Rest Framework
- [Django]-Django – Why doesn't syncdb respect the database router?
- [Django]-Aggregate totals of a ViewSet results in Django Rest Framework
- [Django]-What is the correct way to include subdomains to the Django sitemap urls?