wagtail-generic-chooser
wagtail-generic-chooser copied to clipboard
FR: Allow models to be set as string in AdminChooser
Currently, I'm facing circular dependency in AdminChooser
and solved this by importing the Model in the __init__
like this:
from django.contrib.admin.utils import quote
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from generic_chooser.widgets import AdminChooser
class DepartmentChooser(AdminChooser):
def __init__(self, **kwargs):
from career.models import Department
self.model = Department
super().__init__(**kwargs)
choose_one_text = _('Choose a department')
choose_another_text = _('Choose another department')
link_to_chosen_text = _('Edit this department')
choose_modal_url_name = 'department_chooser:choose'
def get_edit_item_url(self, item):
from career.wagtail_hooks import DepartmentAdmin
admin = DepartmentAdmin()
return admin.url_helper.get_action_url('edit', item.pk)
While it works, it's not really ideal.
Allowing model to be set as String (like SnippetChooserPanel
) and resolving it dynamically will provide great ergonomics I believe.
Seconding this request!
I just had to run down a circular import that I couldn't quite determine the cause of, until I realized that my widgets.py
was importing my choosable class, and my choosable class was defined in the same file as the model that uses my widget in its form panels.