def save(self): obj = super(_EventForm, self).save() # check if this country exists and create if necessary country_raw = self.cleaned_data['country'] # assuming 'country name [SHORT]' format of the name result = re.match('(.*)\[(.*)\]', country_raw) try: country_long = result.group(1).strip() country_short = result.group(2).strip() except (IndexError, AttributeError): country_long = '' country_short = country_raw country = Country.get_or_insert( country_short, long_name = country_long ) obj.country = country # check region region_raw = self.cleaned_data['region'] if region_raw != '': result = re.match('(.*)\[(.*)\]', region_raw) try: region_long = result.group(1).strip() region_short = result.group(2).strip() except (IndexError, AttributeError): region_long = region_raw region_short = region_raw region = Region.get_or_insert( region_short, long_name = region_long, country=country ) obj.region = region else: obj.region = None return obj