Django UpdateView 和 ChoiceField 问题。姜戈 1.11 python 3.6

Django UpdateView and ChoiceField issue. Django 1.11 python 3.6

我在 Django 中实现 ChoiceField 和 UpdateView 时遇到了一个奇怪的问题。我制作了一个小剪辑来展示我面临的问题。请在启用 subtitles/cc 的情况下观看。它将对我面临的问题有一个想法。 https://youtu.be/M36TnlJvrZs。问题是这样的......

CreateView 期间,我将“性别ChoiceField 设置为“女性”。但在 UpdateView 中,它将“性别ChoiceField 预填充为 Male.

但是,ListView 将“性别”字段正确呈现为“女性”。

奇怪的是,django 管理面板根本没有显示“性别”字段的任何值。

所有代码如下:

models.py:

from django.db import models
from django.core.urlresolvers import reverse

gender_choices = (('Male', 'Male'), ('Female', 'Female'))

class Birth(models.Model):
    full_name = models.CharField(max_length = 100)    
    gender = models.CharField(max_length=6, choices=gender_choices)
    date_of_birth = models.DateField()
    place_of_birth = models.CharField(max_length = 50)
    mother_name = models.CharField(max_length = 50)
    father_name = models.CharField(max_length = 50)
    address_at_time_of_birth = models.TextField(max_length = 500)
    permanent_address = models.TextField(max_length = 500)
    registration_no = models.CharField(max_length = 50)
    remarks = models.CharField(max_length = 200)    
    registration_date = models.DateField()
    issue_date = models.DateField()

    def get_absolute_url(self):
        return reverse('birth:birth_update', kwargs={'pk':self.pk})
        #return reverse('birth:birth_home')

    def __str__(self):
        return self.full_name

forms.py:

from django import forms
from .models import *    

class BirthForm(forms.ModelForm):
        full_name = forms.CharField()
        gender = forms.ChoiceField(choices = gender_choices, widget=forms.Select())
        date_of_birth = forms.DateField(widget = forms.DateInput(attrs = {'placeholder':'DD/MM/YYYY'}))
        place_of_birth = forms.CharField()
        mother_name = forms.CharField()
        father_name = forms.CharField()
        address_at_time_of_birth = forms.CharField(widget = forms.Textarea())
        permanent_address = forms.CharField(widget = forms.Textarea())
        registration_no = forms.CharField(required = False)
        registration_date = forms.DateField(required = False, widget = forms.DateInput(attrs = {'placeholder':'DD/MM/YYYY'}))
        remarks = forms.CharField(required = False)
        issue_date = forms.DateField(required = False, widget = forms.DateInput(attrs = {'placeholder':'DD/MM/YYYY'}))


        class Meta:
            model = Birth
            fields = '__all__'

views.py:

from django.views.generic import ListView, CreateView, UpdateView
from .models import *
from .forms import *
from datetime import date


class BirthHome(ListView):
    template_name = 'birth/birth_home.html'
    model = Birth
    context_object_name = 'birth_objects'
    paginate_by = 20

    def get_queryset(self):
        return Birth.objects.all().order_by('-id')


class NewBirth(CreateView):
    model = Birth
    form_class = BirthForm
    #fields = '__all__' 
    template_name = 'birth/birth_add.html'


    def form_valid(self, form):
        obj = form.save(commit = False)
        if not obj.registration_date:
            obj.registration_date = date.today()
        if not obj.issue_date:
            obj.issue_date = date.today()
        if not (date(1900, 1, 1) <= obj.date_of_birth <= date.today()):
            form.add_error('date_of_birth', 'Please enter a valid date')
            return super(NewBirth, self).form_invalid(form)
        obj.full_name = obj.full_name.upper()
        obj.gender = obj.gender.upper()
        obj.place_of_birth = obj.place_of_birth.upper()
        obj.mother_name = obj.mother_name.upper()
        obj.father_name = obj.father_name.upper()
        obj.address_at_time_of_birth = obj.address_at_time_of_birth.upper()
        obj.permanent_address = obj.permanent_address.upper()
        if obj.remarks:
            obj.remarks = obj.remarks.upper()
        self.object = form.save()
        return super(NewBirth, self).form_valid(form)


class BirthUpdate(UpdateView):
    model = Birth
    form_class = BirthForm
    template_name = 'birth/birth_update.html'

    def form_valid(self, form):
        obj = form.save(commit = False)
        if not obj.registration_date:
            obj.registration_date = date.today()
        if not obj.issue_date:
            obj.issue_date = date.today()
        if not (date(1900, 1, 1) <= obj.date_of_birth <= date.today()):
            form.add_error('date_of_birth', 'Please enter a valid date')
            return super(BirthUpdate, self).form_invalid(form)
        obj.full_name = obj.full_name.upper()
        obj.gender = obj.gender.upper()
        obj.place_of_birth = obj.place_of_birth.upper()
        obj.mother_name = obj.mother_name.upper()
        obj.father_name = obj.father_name.upper()
        obj.address_at_time_of_birth = obj.address_at_time_of_birth.upper()
        obj.permanent_address = obj.permanent_address.upper()
        if obj.remarks:
            obj.remarks = obj.remarks.upper()
        self.object = form.save()
        return super(BirthUpdate, self).form_valid(form)

我搜索了很多,也尝试了很多,但都无济于事。严重需要帮助。另外,如果这种方法不正确,正确的工作方法应该是什么?

已解决!

所以经过大量的试验,我意识到了问题所在!!它在 models.py 文件中:

由于我将所有输入都转换为大写,因此 'choices' 元组的值也需要大写。最初 gender_choices 元组是这样读的:

gender_choices = (('Male', 'Male'), ('Female', 'Female'))

而且在我看来,我将性别设为大写,从而导致声明的元组数据和表单数据不匹配。

因此,我将元组更改为:

gender_choices = (('MALE', 'MALE'), ('FEMALE', 'FEMALE'))

工作起来很有魅力!!干杯......并感谢所有的帮助和建议。随时欢迎任何反馈:)