mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-16 01:27:03 +02:00
added related objects to contact object change_view in django admin
This commit is contained in:
parent
7077519837
commit
83c34ceaf0
3 changed files with 69 additions and 0 deletions
|
@ -415,6 +415,7 @@ class ContactAdmin(ListHeaderAdmin):
|
||||||
"contact",
|
"contact",
|
||||||
"email",
|
"email",
|
||||||
]
|
]
|
||||||
|
change_form_template = "django/admin/contact_change_form.html"
|
||||||
|
|
||||||
# We name the custom prop 'contact' because linter
|
# We name the custom prop 'contact' because linter
|
||||||
# is not allowing a short_description attr on it
|
# is not allowing a short_description attr on it
|
||||||
|
@ -452,6 +453,45 @@ class ContactAdmin(ListHeaderAdmin):
|
||||||
readonly_fields.extend([field for field in self.analyst_readonly_fields])
|
readonly_fields.extend([field for field in self.analyst_readonly_fields])
|
||||||
return readonly_fields # Read-only fields for analysts
|
return readonly_fields # Read-only fields for analysts
|
||||||
|
|
||||||
|
def change_view(self, request, object_id, form_url='', extra_context=None):
|
||||||
|
"""Extend the change_view for Contact objects in django admin.
|
||||||
|
Customize to display related objects to the Contact. These will be passed
|
||||||
|
through the extra_context to the template for display to the user."""
|
||||||
|
extra_context = extra_context or {}
|
||||||
|
|
||||||
|
# Fetch the Contact instance
|
||||||
|
contact = models.Contact.objects.get(pk=object_id)
|
||||||
|
|
||||||
|
# initialize related_objects array
|
||||||
|
related_objects = []
|
||||||
|
# for all defined fields in the model
|
||||||
|
for related_field in contact._meta.get_fields():
|
||||||
|
# if the field is a relation to another object
|
||||||
|
if related_field.is_relation:
|
||||||
|
# Check if the related field is not None
|
||||||
|
related_manager = getattr(contact, related_field.name)
|
||||||
|
if related_manager is not None:
|
||||||
|
# Check if it's a ManyToManyField or a reverse ForeignKey/OneToOneField
|
||||||
|
# Do this by checking for a method on the related_manager
|
||||||
|
if hasattr(related_manager, 'get_queryset'):
|
||||||
|
queryset = related_manager.get_queryset()
|
||||||
|
else:
|
||||||
|
queryset = related_manager.all()
|
||||||
|
|
||||||
|
for obj in queryset:
|
||||||
|
# for each object, build the edit url in this view and add as tuple
|
||||||
|
# to the related_objects array
|
||||||
|
app_label = obj._meta.app_label
|
||||||
|
model_name = obj._meta.model_name
|
||||||
|
obj_id = obj.id
|
||||||
|
change_url = reverse('admin:%s_%s_change' % (app_label, model_name), args=[obj_id])
|
||||||
|
related_objects.append((change_url, obj))
|
||||||
|
|
||||||
|
# set the related_objects array in extra_context
|
||||||
|
extra_context['related_objects'] = related_objects
|
||||||
|
|
||||||
|
return super().change_view(request, object_id, form_url, extra_context=extra_context)
|
||||||
|
|
||||||
|
|
||||||
class WebsiteAdmin(ListHeaderAdmin):
|
class WebsiteAdmin(ListHeaderAdmin):
|
||||||
"""Custom website admin class."""
|
"""Custom website admin class."""
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
{% extends 'admin/change_form.html' %}
|
||||||
|
{% load custom_filters %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
{% if related_objects %}
|
||||||
|
<div>
|
||||||
|
<h2>Related Objects:</h2>
|
||||||
|
<ul>
|
||||||
|
{% for url, obj in related_objects %}
|
||||||
|
<li>
|
||||||
|
{{ obj|class_name }}:
|
||||||
|
<a href="{{ url }}">
|
||||||
|
{{ obj }}
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{{ block.super }}
|
||||||
|
|
||||||
|
{% endblock %}
|
|
@ -7,6 +7,11 @@ register = template.Library()
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@register.filter
|
||||||
|
def class_name(value):
|
||||||
|
return value.__class__.__name__
|
||||||
|
|
||||||
|
|
||||||
@register.filter(name="extract_value")
|
@register.filter(name="extract_value")
|
||||||
def extract_value(html_input):
|
def extract_value(html_input):
|
||||||
match = re.search(r'value="([^"]*)"', html_input)
|
match = re.search(r'value="([^"]*)"', html_input)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue