diff --git a/dbmail/admin.py b/dbmail/admin.py index 8e3658b..068a6d6 100644 --- a/dbmail/admin.py +++ b/dbmail/admin.py @@ -183,6 +183,20 @@ def get_prepopulated_fields(self, request, obj=None): return super(MailTemplateAdmin, self).get_prepopulated_fields( request, obj) + def get_queryset(self, request): + return super(MailTemplateAdmin, self).get_queryset(request) \ + .select_related('from_email', 'category') + + def formfield_for_foreignkey(self, db_field, request, **kwargs): + formfield = super(MailTemplateAdmin, self).formfield_for_foreignkey( + db_field, request, **kwargs) + if db_field.name == 'category': + key = '_category_choices_cache' + if not hasattr(request, key): + setattr(request, key, list(formfield.choices)) + formfield.choices = getattr(request, key) + return formfield + class MailLogEmailInline(admin.TabularInline): readonly_fields = [field.name for field in MailLogEmail._meta.fields]