开发者

Django and ModelForm. How to change IntegerField to dropdown box

I have a model that looks like this

class RSVP (models.Model):
    def __unicode__(self):
            return self.firstName + " " + self.lastName
    firstName = models.CharField(max_length=30)
    lastName = models.CharField(max_length=30)
    rsvpID = models.CharField(max_length=9, unique = True)
    allowedAdults = models.IntegerField(default = 2)
    allowedChildren = models.IntegerField(default = 0)
    adultsAttending = models.IntegerField(default = 0)
    childrenAttending = models.IntegerField(default = 0)

and I have a ModelForm that looks like this

class RsvpForm(ModelForm):
    class Meta:
        model = RSVP
        exclude= ('firstName', 'lastName', 'allowedAdults', 'allowedChildren')

What I would like to happen is that instead of a text field for the adults开发者_高级运维Attending, a dropdown box with the values 0 to allowedAdults shows up. This is for a wedding rsvp site and I'd like to set the max number of +1's an invitee can bring on an individual basis

Any thoughts on how to go about this?


I'm thinking you want to fork the allowed children/ adults as well as the name to another model:

models.py

class Invited(models.Model):
    f_name = models.CharField()
    l_name = models.CharField()
    allowed_adults = models.IntegerField()
    allowed_children = models.IntegerField()

class RSVP(models.Model):
    invited = models.ForeignKey(Invited)
    adults_attending = models.IntegerField()
    children_attending = models.IntegerField()

Then you would create the invited objects and assign the allowed adults and children. And the RSVP form would take those number into account when generating the choices for your drop down box.

The drop down can be implemented by overriding the IntegerField widget with a ChoiceField

forms.py

class InvitedForm(forms.ModelForm):
    class Meta:
        model = Invited

class RSVPForm(forms.ModelForm):
    class Meta:
        model = RSVP
        exclude = ['invited',]

    def __init__(self, *args, **kwargs):
        max_adults = kwargs.pop('max_adults',2) #default to 2 if no max set
        max_children = kwargs.pop('max_children',2) #default to 2 if no max set

        super(RSVPForm, self).__init__(*args, **kwargs)

        adult_choices = ( (x,str(x)) for x in range(max_adults+1)) )
        children_choices = ( (x,str(x)) for x in range(max_children+1)) )

        self.fields['adults_attending'] = forms.ChoiceField(choices = adult_choices)
        self.fields['children_attending'] = forms.ChoiceField(choices = children_choices)

views.py

def rsvp_view(request, invited_id):
    invited = get_object_or_404(Invited, pk=invited_id)
    if request.method=='POST':
        form = RSVPForm(request.POST, max_adults=invited.allowed_adults,
                        max_children=invited.allowed_children)
        if form.is_valid():
            rsvp = form.save(commit=False)
            rsvp.invited = invited
            rsvp.save()
            return HttpResponse("Success")
    else:
        form = RSVPForm(max_adults=invited.allowed_adults, max_children=invited.allowed_children)

    context = { 'form':form,
                'invited':invited }
    return render_to_response('rsvp.html', context,
                               context_instance=RequestContext(request))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜