I have a model
class Group(models.Model):
active = models.BooleanField(null=False, blank=False, default=True)
and its admin pages
class GroupAdmin(admin.ModelAdmin):
change_form_template = "admin/group/group.html"
form = GroupAdminForm
def change_view(self, request, object_id, form_url='', extra_context=None):
extra_context = extra_context or {}
extra_context['group_data'] = self.get_info(object_id)
return super(GroupAdmin, self).change_view(
request, object_id, form_url, extra_context=extra_context,
)
class GroupAdminForm(ModelForm):
class Meta:
model = Group
fields = '__all__'
def clean_active(self):
active = self.cleaned_data['active']
if 'active' in self.changed_data and not active and OtherCondition:
raise ValidationError('Group must stay active because of OtherCondition')
return active
which requires the change_view template.
Because of the change_view template, validation errors are not showing up.
How do I throw a validation error and have it show up on Django admin? Is there a way to do this using ValidationError? Is it by changing the change_view template?
- When I throw it on Group.save() on the Model, it breaks the page, instead of not-saving and telling the user to fix the error.
- At first I hadn't used an AdminForm, but using it means the validation is run, and it requests a change (Please correct the error below.), but it doesn't show the ValidationError message.
This question is similar but recommends Messages, but I'd like to know if there's another way: Raise django admin validation error from a custom view
I don't think I'm including field errors specifically, but I think it should be including them through the django supplied html:
{% block field_sets %}
{% for fieldset in adminform %}
{% include "admin/includes/fieldset.html" %}
{% endfor %}
{% endblock %}
and admin/includes/fieldset.html has {{ line.errors }}
. Maybe it's to do with passing the error to the change_view?
group.html
as requested.
{% extends "admin/change_form.html" %}
{% block field_sets %}
{# stuff #}
{{ block.super }}
{% endblock %}
{% block inline_field_sets %}
{% for inline_admin_formset in inline_admin_formsets %}
{# stuff #}
{% endfor %}
{% endblock %}
{# M2M Preview #}
{% block after_related_objects %}
{# stuff #}
{% endblock %}