Reputation: 375
Django==1.10
I want to use a models.BooleanField
as a forms.ChoiceField
in the admin
app.
from __future__ import unicode_literals
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=255)
bool_field = models.BooleanField()
from django.contrib import admin
from .models import MyModel
from .forms import MyModelForm
@admin.register(MyModel)
class TemplateAdmin(admin.ModelAdmin):
form = MyModelForm
list_display = ['name','bool_field']
from django import forms
class MyModelForm(forms.ModelForm):
bool_choices = ((True, "Yes"),(False, "No"))
bool_field = forms.ChoiceField(choices=bool_choices)
Everything displays just like I want, but in fact every time I save the instance the History
link shows that the bool_field
was changed even if it wasn't.
I tried to change the bool_field
to TypedChoiceField
with coerce=bool
, but it doesn't work well. After I change and save the instance the value remains the same.
Please advise what I should probably change in order to get History
working right.
Upvotes: 2
Views: 629
Reputation: 2002
Try and specify that the bool_field
is not required, like so:
bool_field = forms.ChoiceField(choices=bool_choices, required=False)
Upvotes: 1