Django admin return custom error message during model saving

12,460

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.

Share:
12,460

Related videos on Youtube

brsbilgic
Author by

brsbilgic

Updated on June 04, 2022

Comments

  • brsbilgic
    brsbilgic almost 2 years

    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

  • brsbilgic
    brsbilgic over 12 years
    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 :)
  • Brandon Taylor
    Brandon Taylor over 12 years
    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.
  • andilabs
    andilabs about 10 years
    Could you suggest me solution for case when I want to check e.g uploaded file with some extra method and then return errors?
  • Oliver Matthews
    Oliver Matthews almost 10 years
    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.
  • Kix Panganiban
    Kix Panganiban almost 10 years
    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. :)
  • Brandon Taylor
    Brandon Taylor almost 10 years
    Sorry I left off the admin registration. I've updated my answer.
  • pinghsien422
    pinghsien422 almost 10 years
    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.