Reputation: 541
Models.py
class user(AbstractUser):
salary_number = models.IntegerField(unique=True, null=True, blank=True)
Admin.py
def save_model(self, request, obj, form, change):
if obj.salary_number == None:
raise ValidationError("ERROR salary number!")
....
obj.save()
I'm trying to show error for user if they forget to fill salary number, but I got error ValidationError at /admin/information/user/add/. How can I fixed that?
I have a reason to not set salary_number null & blank = False.
Upvotes: 2
Views: 934
Reputation: 477853
The simplest way is probably to make a small ModelForm
:
from django import forms
from django.core.exceptions import ValidationError
class UserModelForm(forms.ModelForm):
def clean_salary_number(self):
data = self.cleaned_data['salary_number']
if data is None:
raise ValidationError('ERROR salary number!')
return data
and then plug this in in the ModelAdmin
:
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
# …
form = UserModelForm
Upvotes: 2