Reputation: 8894
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?
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 %}
Upvotes: 2
Views: 1512
Reputation: 628
I think form validation is a good idea in these type of situations.
forms.py
class YourForm(forms.ModelForm):
def clean(self):
super(YourForm, self).clean()
data1 = self.cleaned_data.get('data1')
data2 = self.cleaned_data.get('data2')
# Add validation condition here
# if validation error happened you can raise the error
# and attach the error message with the field you want.
self.add_error('field_name', 'error message')
In admin.py
class YourAdminClass(admin.ModelAdmin):
form = YourForm
Upvotes: 3