8

I would like to return some custom error messages in save_model function of Django admin page.

class EmployerAdmin(admin.ModelAdmin):
  exclude = ('update_user','updatedate','activatedate','activate_user')

  def save_model(self, request, obj, form, change):
    if obj.department != None and obj.isDepartmentSuggested:
        obj.isDepartmentSuggested =False
    else:
       return "You don't set a valid department. Do you want to continue ?"

    obj.update_user = request.user
    obj.updatedate = datetime.datetime.now()
    obj.save()

Of course, Else part isn't correct but I want to illustrate what I want.

I am glad to suggest me a way or document to do that. Thanks

2 Answers 2

15

You need to use a form to do your validation in your EmployerAdmin:

#forms.py
from your_app.models import Employer

class EmployerAdminForm(forms.ModelForm):
    class Meta:
        model = Employer

    def clean(self):
        cleaned_data = self.cleaned_data
        department = cleaned_data.get('department')
        isDepartmentSuggested = cleaned_data.get('isDepartmentSuggested')
        if department == None and not isDepartmentSuggested:
            raise forms.ValidationError(u"You haven't set a valid department. Do you want to continue?")
        return cleaned_data

#admin.py
from django.contrib import admin
from your_app.forms import EmployerAdminForm
from your_app.models import Employer

class EmployerAdmin(admin.ModelAdmin):
    exclude = ('update_user','updatedate','activatedate','activate_user')
    form = EmployerAdminForm

admin.site.register(Employer, EmployerAdmin)

Hope that helps you out.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks. That's what I want. Could you give me any idea how can I render a pop-up box or Continue button next to the error message ? Or should I :)
First, you'll need to set the related fields as not required, and/or nullable if that applies, since you want to let people continue, even if they don't make a selection. To do the pop-up, a JavaScript "confirm" dialog is what you need. Take a look at: jqueryui.com/demos/dialog/#modal-confirmation to get some ideas.
Could you suggest me solution for case when I want to check e.g uploaded file with some extra method and then return errors?
Since form validation and save_model isn't run atomically, (I just tested this, I'm on django v1.5) in some cases it is necessary to perform final validation inside save_model() just to make sure you're not performing save using stale data. A concurrent operation might have changed the value AFTER clean() but BEFORE save_model(), and if the allowed value is dependent on the previous value, validating inside clean() is going to be problematic. The only solution is to perform an select_for_update() inside the save_model() to lock the row, and then perform final validation just before saving.
0

I'm using Django 1.6.3, and I'd like to add to Brandon's answer.

Add admin.site.register(Employer, EmployerAdmin) as a separate line below the EmployerAdmin class; that is, below form = EmployerAdminForm, unindented.

It took me some time to figure out why Brandon's answer wasn't working for me and the validations weren't running, apparently, you just need to register it on admin first.

Cheers.

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
Problem is, I don't have enough rep points, and I felt like this should be shared to save others the hassle which I encountered earlier. :)
Sorry I left off the admin registration. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.