Exemplo n.º 1
0
    def clean_city(self):
        if type(self.cleaned_data['zip']) != type(Zip()):
            self.clean_zip(self)

        try:
            cityobj = City.objects.get(name=self.cleaned_data['city'], zip__pk__in=[self.cleaned_data['zip'].pk,])
        except City.DoesNotExist:
            cityobj = City()
            cityobj.name = self.cleaned_data['city']
            cityobj.save()
            cityobj.zip.add(self.cleaned_data['zip'])
            cityobj.save()
        return cityobj
Exemplo n.º 2
0
    def clean(self):
        cleaned_data = super(EditRetailerForm, self).clean()
        zip = cleaned_data.get('zip')
        city = cleaned_data.get('city')

        if zip and city:
            try:
                cityobj = City.objects.get(name=city, zip__pk__in=[zip.pk,])
            except City.DoesNotExist:
                cityobj = City()
                cityobj.name = city
                cityobj.save()
                cityobj.zip.add(zip)
                cityobj.save()

            cleaned_data['city'] = cityobj

        return cleaned_data