Opened 11 years ago

Closed 11 years ago

#20209 closed Bug (worksforme)

fields is empty after post with RadioWidget

Reported by: strelnikovdmitrij Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords:
Cc: bmispelon@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

---forms.py---
class ApplicantForm(forms.ModelForm):

# gender = forms.ChoiceField(choices=Gender.CHOICES, widget=forms.RadioSelect)

class Meta:

exclude = (...)
model = Applicant

widgets = {

'gender': forms.RadioSelect,

}

---view.py---
def compose_full(request, tmpl='applicant/compose.html'):

data = {}

applicant = Applicant()
applicant.created_by = request.user
applicant.ip = get_ip(request)

appl_frm = ApplicantForm(request.POST or None, instance=applicant)
att_frm = AppCVForm(request.POST or None, request.FILES or None)

if appl_frm.is_valid() and att_frm.is_valid():

....

---models.py---
class Applicant(models.Model):
...
gender = models.IntegerField(choices=Gender.CHOICES, default=Gender.UNSPECIFIED)
...

after POST form, no value of gender is selected

all works fine with default widget, if change widget to Meta class, or change it in form init method, or define it in form class (commented line)
RadioSelect or RadioSelect() providing same result (if no exception is raised)

Change History (4)

comment:1 by Baptiste Mispelon, 11 years ago

Resolution: worksforme
Status: newclosed

Hi,

I'm marking this as "worksforme" because I could not reproduce the issue described in the report.

Here's the code that I used:

# models.py
from django.db import models

GENDERS = [
    (1, 'unspecified'),
    (2, 'male'),
    (3, 'female'),
]

class Applicant(models.Model):
    gender = models.IntegerField(choices=GENDERS, default=1)

# forms.py
from django import forms

from .models import Applicant

class ApplicantForm(forms.ModelForm):
    class Meta:
        fields = ('gender',)
        model = Applicant
        widgets = {
            'gender': forms.RadioSelect,
        }

# views.py
from django.shortcuts import render
from django.http import HttpResponse

from .forms import ApplicantForm

def create_applicant(request):
    form = ApplicantForm(request.POST or None)
    if form.is_valid():
        applicant = form.save()
        return HttpResponse(applicant.get_gender_display()) # No issue here
    return render(request, 'bug20209/applicant_form.html', {'form': form})

If you can provide a simple bit of code that demonstrate your issue, feel free to reopen this ticket.

Thanks.

comment:2 by Baptiste Mispelon, 11 years ago

Cc: bmispelon@… added

comment:3 by strelnikovdmitrij, 11 years ago

Resolution: worksforme
Status: closednew

Hi,
due to default choice at applicant gender it not raise issue,

but if you will use instance for form, and your form will not be validated on post, the value of gender (which you choose before post) will not displayed with RadioSelect widget.

comment:4 by Baptiste Mispelon, 11 years ago

Resolution: worksforme
Status: newclosed

Hello again,

I'm still having trouble indentifying the issue you're talking about.

After your comment, I updated the code to this:

# view
def update_applicant(request, pk):
    instance = get_object_or_404(Applicant, pk=pk)
    form = ApplicantForm(request.POST or None, instance=instance)
    if form.is_valid():
        applicant = form.save()
        return HttpResponse(applicant.get_gender_display()) # No issue here
    return render(request,
        'bug20209/applicant_form.html',
        {'form': form, 'object': instance}
    )

# form
class ApplicantForm(forms.ModelForm):
    foo = forms.CharField(required=True) # to trigger validation error

    class Meta:
        fields = ('gender',)
        model = Applicant
        widgets = {
            'gender': forms.RadioSelect,
        }

But the code still behaves as intended: the RadioSelect widget correctly displays the instance's gender (or whatever was selected in the case of an invalid form).

I'm closing this again. If you can produce a minimal example reproducing your issue, please do reopen.

Note: See TracTickets for help on using tickets.
Back to Top