Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django form field validation - How to tell if operation is insert or update?

I'm trying to do this in Django: When saving an object in the Admin I want to save also another object of a different type based on one of the fields in my fist object. In order to do this I must check if that second object already exists and return an validation error only for the particular field in the first object if it does. My problem is that I want the validation error to appear in the field only if the operation is insert.

How do I display a validation error for a particular admin form field based on knowing if the operation is update or insert?

P.S. I know that for a model validation this is impossible since the validator only takes the value parameter, but I think it should be possible for form validation.

like image 789
Al Bundy Avatar asked Oct 03 '12 11:10

Al Bundy


People also ask

When saving How can you check if a field has changed Django?

To check if a field has changed when saving with Python Django, we can override the `init method of the model class to keep a copt of the original value. to add the __init__ method that sets the __original_name variable to self.name to keep the original name value.

How do I validate fields in Django?

Django forms submit only if it contains CSRF tokens. It uses uses a clean and easy approach to validate data. The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is cleaned_data Django?

cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).

What can be used to validate all model fields if any field is to be exempted from validation provide it in the exclude parameter?

clean_fields() method documentation: This method will validate all fields on your model. The optional exclude argument lets you provide a list of field names to exclude from validation. It will raise a ValidationError if any fields fail validation.


1 Answers

This ca be done by writing a clean_[name_of_field] method in a Django Admin Form. The insert or update operation can be checked by testing self.instance.pk.

class EntityAdminForm(forms.ModelForm):
    def clean_field(self):
        field = self.cleaned_data['field']
        insert = self.instance.pk == None
        if insert:
            raise forms.ValidationError('Some error message!')
        else:
            pass
        return field

class EntityAdmin(admin.ModelAdmin):
    form = EntityAdminForm

You have to use then the EntityAdmin class when registering the Entity model with the Django admin:

admin.site.register(Entity, EntityAdmin)
like image 88
Al Bundy Avatar answered Sep 21 '22 03:09

Al Bundy