Monday, November 26, 2018

How to get a multiple select widget for the Admin without a queryset

In the Admin I would like to provide the user with checkboxes against a list of options not stored in the database.

Specifically, I want to retrieve a comma separated list of integers from a model field choices attribute. The model field looks like this:
    menu_links = models.CharField(max_length=LARGE, blank=True,          choices=MENU_LINKS,          default='',          validators=[int_list_validator],          verbose_name='Menu data links',          help_text="These selections control which URLs are displayed in the "          "'Substances and mixtures' menu",      )
I'm seeing two problems.

One is getting such a widget to appear in an admin.StackedInline class.

Two is creating the data in the above models.CharField from choices=MENU_LINKS

Here it is ...
MENU_LINKS = [      (1, "ChemIDplus"),      (2, "ChemSpider"),      (3, "NIST Webbook"),  ]

In the admin I have tried

class ProfileFilteredSelectMultiple(admin.widgets.FilteredSelectMultiple):
    def __init__(self, verbose_name='Menu links', is_stacked=False, *args, **kwargs):          args += (verbose_name,)          args += (is_stacked,)          super(ProfileFilteredSelectMultiple, self).__init__(*args, **kwargs)
      
Then in the nested StackedInline class ...

    class CompanyProfileInline(admin.StackedInline):
        model = CompanyProfile          formfield_overrides = {              CharField: {'widget': ProfileFilteredSelectMultiple},          }          readonly_fields = (('modified', 'modified_by',))          fieldsets = (              ('More detail for company profile', {                  'classes': ('collapse', 'wider'),                  'fields': (                       'menu_links',                       ...
Any hints or pointers will be appreciated.

Thanks

Mike

No comments:

Post a Comment