Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to present flags in django admin list view ( flags which are active ) #101

Open
maharshi-trendlyne opened this issue Mar 4, 2020 · 1 comment

Comments

@maharshi-trendlyne
Copy link

maharshi-trendlyne commented Mar 4, 2020

I have a model with BitField like this:

class MyModel(models.Model):
        notifications = BitField(flags=('facebook', 'phone', 'gmail'))

Say facebook, gmail are active, then in list view(django-admin) want those flag label comma separated.

@maharshi-trendlyne maharshi-trendlyne changed the title How to present flags in django admin list view ( flags which are active) How to present flags in django admin list view ( flags which are active ) Mar 4, 2020
@mromagnoli
Copy link

Way too late but perhaps useful for someone else.

You can define a method in your AdminModel to represent them as you want, and then list it in the list_display attr.
For instance, what I did is to list it in its own column named Features, and if the flag is ON show a ✅ , and if it's OFF a ❌

list_display = (
   "features_repr"
)
@admin.display(description="Features")
    def features_repr(self, obj):
        repr = ""
        check_mark = "✅"
        cross_mark = "❌"

        for feature_name, is_set in obj.features.iteritems():
            feature_name = obj.features.get_label(feature_name)
            status_mark = check_mark if is_set else cross_mark

            repr += f"<b>{feature_name}:</b> {status_mark}<br>"

        return format_html(repr)

I called my model field features but your's should be obj.notifications

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants