My question is about how to validate one field based on the value of
another field in a modelform,
but I don't want to show the second field in the form.

I've got a Ticket model with a foreign key field to Event:

class Ticket(models.Model):  # General Admission or assigned seating
section
    event = models.ForeignKey(Event)
    sales_end = models.DateField()
   ...# lots more

class TicketForm(ModelForm):
    patron_type = forms.ModelChoiceField(empty_label=None, queryset =
None)
    event = forms.ModelChoiceField(widget=forms.HiddenInput(),
queryset = Event.objects.all())
    #event = forms.IntegerField(widget=forms.HiddenInput())
    class Meta:
        model = Ticket
        exclude = []
    def __init__(self, *args, **kwargs):
        super(TicketForm, self).__init__(*args, **kwargs)
        #self.fields['event'].id = event_id
        self.fields['patron_type'].queryset = PatronType.objects.all()
    def clean(self):
        sales_end = self.cleaned_data.get("sales_end")
        #event = self.cleaned_data.get("event")

        # want to validate sales_end date against event.date,
        #if sales_end > event.date:
        #    raise forms.ValidationError("Sales end date must be on or
before the day of the event")
        return cleaned_data

In my view I know the event before the form is created:

formTicket = TicketForm(initial={'event': event})

My question, how do I make the event not appear in the form at all,
but be available in the clean function so that I can validate the
sales_end field properly and report the validation error?

Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to