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

11 views
Skip to first unread message

Mike Dewhirst

unread,
Nov 26, 2018, 10:24:27 PM11/26/18
to Django users
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

shiva kumar

unread,
Nov 26, 2018, 11:58:20 PM11/26/18
to django...@googlegroups.com
Can you explain what ur problem briefly.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a49d990e-67fc-3327-4205-a3b843f7f7ae%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Nov 27, 2018, 12:28:50 AM11/27/18
to django...@googlegroups.com
On 27/11/2018 3:57 PM, shiva kumar wrote:
> Can you explain what ur problem briefly.
In the Admin I show a list of substance names and against each one there
can be a list of clickable links to public chemical databases. When the
user clicks one of those links the database opens in a new browser page
with that substance showing. The user may then browse that public
database for info on that substance.

Currently we have hard-coded three of 14 or 15 different public chemical
databases. Different users have different preferences so I want to allow
them to select their preferred one or two in their company profile and
tweak the Admin to check the profile and display those instead of the
hard-coded three.

Since sending that earlier email I have adjust the field type to
ListField (import CharField as ListField) so that I know there is only
one field in the entire project which is a ListField. This is for CSS
and formfield_overrides.

>
> On Tue 27 Nov, 2018 8:54 am Mike Dewhirst <mi...@dewhirst.com.au
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/a49d990e-67fc-3327-4205-a3b843f7f7ae%40dewhirst.com.au?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMsYeuE%3Dcxh-AC3e%2BZ9z57TEPMqbGO5CfohyVxuQSFyzJML0zw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMsYeuE%3Dcxh-AC3e%2BZ9z57TEPMqbGO5CfohyVxuQSFyzJML0zw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages