django exclude self from queryset for validation

Viewed 7933

I am working with my own clean method to see if some other table already had a field with the same string. This all goes fine as long as i am creating one, but when i try to edit it, it find "itself" and gives back the error. Now am i wondering how can i exclude the instance itself in my clean method

def clean_name(self):
    raw_data = self.cleaned_data['name']
    data = raw_data.title()

    if Country.objects.filter(name=data).exists():
        raise forms.ValidationError(("There is already a country with the name: %s") % data)
    if Province.objects.filter(name=data).exists():
        raise forms.ValidationError(("There is already a province with the name: %s") % data)
    if Region.objects.filter(name=data).exists():
        raise forms.ValidationError(("There is already a region with the name: %s") % data) 
    return data

i know there is a .exclude() but that needs a variable to be passed along with it, not sure how i could get that along with my clean method

1 Answers
Related