mirror of
https://github.com/jazzband/django-categories.git
synced 2026-03-16 22:30:24 +00:00
[-] Retrieve content types lazily in Generic Relations admin
This commit is contained in:
parent
7fa98fd58f
commit
5dda5348c0
1 changed files with 14 additions and 5 deletions
|
|
@ -1,23 +1,32 @@
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
class GenericCollectionInlineModelAdmin(admin.options.InlineModelAdmin):
|
class GenericCollectionInlineModelAdmin(admin.options.InlineModelAdmin):
|
||||||
ct_field = "content_type"
|
ct_field = "content_type"
|
||||||
ct_fk_field = "object_id"
|
ct_fk_field = "object_id"
|
||||||
|
|
||||||
def __init__(self, parent_model, admin_site):
|
def get_content_types(self):
|
||||||
super(GenericCollectionInlineModelAdmin, self).__init__(parent_model, admin_site)
|
|
||||||
ctypes = ContentType.objects.all().order_by('id').values_list('id', 'app_label', 'model')
|
ctypes = ContentType.objects.all().order_by('id').values_list('id', 'app_label', 'model')
|
||||||
elements = ["%s: '%s/%s'" % (x, y, z) for x, y, z in ctypes]
|
elements = {}
|
||||||
self.content_types = "{%s}" % ",".join(elements)
|
for x, y, z in ctypes:
|
||||||
|
try:
|
||||||
|
elements[x] = reverse("admin:%s_%s_changelist" % (y, z))
|
||||||
|
except NoReverseMatch:
|
||||||
|
continue
|
||||||
|
return json.dumps(elements)
|
||||||
|
|
||||||
def get_formset(self, request, obj=None, **kwargs):
|
def get_formset(self, request, obj=None, **kwargs):
|
||||||
result = super(GenericCollectionInlineModelAdmin, self).get_formset(request, obj, **kwargs)
|
result = super(GenericCollectionInlineModelAdmin, self).get_formset(request, obj, **kwargs)
|
||||||
result.content_types = self.content_types
|
result.content_types = self.get_content_types()
|
||||||
result.ct_fk_field = self.ct_fk_field
|
result.ct_fk_field = self.ct_fk_field
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
class Media:
|
||||||
|
js = ('contentrelations/js/genericlookup.js', )
|
||||||
|
|
||||||
|
|
||||||
class GenericCollectionTabularInline(GenericCollectionInlineModelAdmin):
|
class GenericCollectionTabularInline(GenericCollectionInlineModelAdmin):
|
||||||
template = 'admin/edit_inline/gen_coll_tabular.html'
|
template = 'admin/edit_inline/gen_coll_tabular.html'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue