Hi there,

can someone help me understand why the following code (posted also
here: http://dpaste.com/4867/) skips validation in clean_city entirely?
Am i not trapping the error correctly by evaluating against None?

I need to say that if a city is specified you can't specify a region or
a province... in other words either u specify a city OR you specify a
province OR you specify a region.

The application is a bug tracker for countries (in my case i want to
track down all Italian bugs so we'll have a Django scaling story to
tell too since they are way too many :) and these fields should help
define the *scope* of the bug.

Also a weird behavior is that if i specify all 3 fields city, province
and region only province and region get saved in the db.

Thanks,
Lorenzo

P.S I've just posted the essential code

from django.newforms import *
from buggato.main.models import *
from django.shortcuts import render_to_response
from django.http import HttpResponse, HttpResponseRedirect, Http404,
HttpRequest

all_cities = [(c.id, c.city) for c in
City.objects.all().order_by("city")[:10]]
all_cities.insert(0, ("", "-- select --"))
all_regions = [(r.id, r.region) for r in
Region.objects.all().order_by("region")]
all_regions.insert(0, ("", "-- select --"))
all_provinces = [(p.id, p.province) for p in
Province.objects.all().order_by("province")]
all_provinces.insert(0, ("", "-- select --"))
all_departments = [(d.id, d.name) for d in
Department.objects.all().order_by("name")]
all_departments.insert(0, ("", "-- select --"))

class BugForm(Form):
    title = CharField()
    severity = IntegerField(widget=Select(choices=BUG_SEVERITY))
    department = IntegerField(widget=Select(choices=all_departments),
required=False)
    description = CharField(widget=Textarea())
    city = IntegerField(widget=Select(choices=all_cities),
required=False)
    address = CharField(required=False)
    province = IntegerField(widget=Select(choices=all_provinces),
required=False)
    region = IntegerField(widget=Select(choices=all_regions),
required=False)


    def clean_city(self):
        if ((self.clean_data.get("city") != None) and
(self.clean_data.get("province") != None)):
            raise ValidationError(u"Since you specified a city you
can't specify a province")
        elif ((self.clean_data.get("city") != None) and
(self.clean_data.get("region") != None)):
            raise ValidationError(u"Since you specified a city you
can't specify a region")

def new_bug(request):
    if request.method=="POST":
        form = BugForm(request.POST)
        if form.is_valid():
            b = Bug(title=form.clean_data["title"],
                    severity=form.clean_data["severity"],
                    description=form.clean_data["description"],
                    city_id=form.clean_data["city"],
                    address=form.clean_data["address"],
                    province_id=form.clean_data["province"],
                    region_id=form.clean_data["region"],
                    assigned_to_id=form.clean_data["department"])
            b.save()
            return HttpResponse("Object saved")
        else:
            context = dict(form=form)
            return render_to_response("new_bug.html", context)
    else:
        form = BugForm()
        context = dict(form=form)
        
    return render_to_response("new_bug.html", context)


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to