21π
β
One way to do that is by overriding the ModelForm for the admin page. That allows you to write custom validation methods and return errors of your choosing very cleanly. Like this in admin.py:
from django.contrib import admin
from models import *
from django import forms
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
def clean_points(self):
points = self.cleaned_data['points']
if points.isdigit() and points < 1:
raise forms.ValidationError("You have no points!")
return points
class MyModelAdmin(admin.ModelAdmin):
form = MyForm
admin.site.register(MyModel, MyModelAdmin)
Hope that helps!
π€Gabriel Hurley
5π
Iβve used the built-in Message system for this sort of thing. This is the feature that prints the yellow bars at the top of the screen when youβve added/changed an object. You can easily use it yourself:
request.user.message_set.create(message='Message text here')
See the documentation.
π€Daniel Roseman
- How to show database errors to user in Django Admin
- A Simple View to Display/Render a Static image in Django
- How to add data-attribute to django modelform modelchoicefield
- Shouldn't django's model get_or_create method be wrapped in a transaction?
2π
Django versions < 1.2 https://docs.djangoproject.com/en/1.4/ref/contrib/messages/
from django.contrib import messages
messages.add_message(request, messages.INFO, 'Hello world.')
π€zzart
- <django.db.models.fields.related.RelatedManager object at 0x7ff4e003d1d0> is not JSON serializable
- AbstractUser Django full example
- Token authentication does not work in production on django rest framework
- Django: efficient template/string separation and override
- Set numbers of admin.TabularInline in django admin
Source:stackexchange.com