python - Django form sporadic validation -
i have form accessed class-based generic updateview. validates, among other things, event being updated not on same date another.
the trouble is, when updating fields, e.g. location, changes processed without error being raised, updating others, e.g. name, throws validation errors event date clashing event, errors should occur when location edited.
why editing fields raise validation errors , not editing other fields? validation should check them all.
my view:
class eventeditview(updateview): template_name = "edit_event.html" pk_url_kwarg='event_id' model = event form_class = eventeditform
which uses form:
class eventeditform(forms.modelform): class meta: model = event def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', none) super(eventeditform, self).__init__(*args, **kwargs) name = forms.charfield(max_length=1024, initial="give short, descriptive name") location = forms.charfield(max_length=1024, initial="be specific, give online map link") datetimeoptions = { 'format': 'dd/mm/yyyy hh:ii p', 'autoclose': 'true', 'showmeridian': 'true', } date = forms.datetimefield(label="date , time", widget=datetimewidget(options=datetimeoptions, attrs={'id':"date-time"})) host_act = forms.charfield(max_length=1024, initial="what act bring, if any?", required=false) description = forms.charfield(required=false, max_length=10240) def clean_location(self): cd = self.cleaned_data location = cd.get('location') if location == "be specific, give online map link" or '': raise forms.validationerror("please enter location") return location def clean_name(self): cd = self.cleaned_data name = cd.get('name') other_names = event.objects.proposed(datetime.now) if name == "give short, descriptive name" or '': raise forms.validationerror("please enter name") return name def clean(self): """ check there not event on @ same time , place. check user has not committed event on same date, somewhere else. """ cleaned_data = super(eventeditform, self).clean() event_start_estimate = cleaned_data.get("date") - timedelta(hours=3) event_end_estimate = event_start_estimate + timedelta(hours=7) location = cleaned_data.get("location") events_on_date = event.objects.\ filter(date__range=[event_start_estimate,event_end_estimate]) events_at_location_on_date = events_on_date.filter(location=location) # check event clash not event clashing # events_with_same_date_and_location_id = events_at_location_on_date.values()[0]['id'] # this_event_id = self.instance.id try: if events_with_same_date_and_location_id == this_event_id: events_at_location_on_date = false except: pass if events_at_location_on_date: raise forms.validationerror("there event on \ date.") user = self.request print user events_on_date_user_has_commit = events_on_date.filter(host__exact=user) if events_on_date_user_has_commit: raise forms.validationerror("you committed event\ on date.") return cleaned_data
you have few errors in code may or may not related problem.
your if statement in clean_location
false because ''
false. think want this:
if name in ["give short, descriptive name", '']:
secondly, same thing in clean_name
. should be:
if name in ["give short, descriptive name", '']:
third, in clean
, using self.request
user object:
user = self.request
should be:
user = self.request.user
maybe these changes fix problem.
Comments
Post a Comment