Django Forms : How to show objects in a dropbox based on the user's group(in the Django admin)

I have a list of all categories mentioned in the models.py. I'm trying to filter categories based on the group defined in django admin.

Can somebody suggest me how to get this done. Please let me know if you need anymore info.

Thanks in advance for your inputs. models.py

class Post(models.Model):
# No Role Required
WATCHLIST = "Watchlist"
LESSON = "Lesson/Review"
GENERAL = "General"
# Blogger Role Required
ANALYSIS = "Analysis"
MILESTONE = "Milestone"
FEATURES = "Features"
TUTORIALS = "Tutorials"
CAREERS = "Careers"
COMMUNITY = "Community"
# Founder Role Required
# FOUNDER = "Founders Journey"

CATEGORY_CHOICES = [
    # No Role Required
    (WATCHLIST, "Watchlist"),
    (LESSON, "Lesson/Review"),
    (GENERAL, "General"),
    # Blogger Role Required
    (ANALYSIS, 'Analysis'),
    (MILESTONE, "Milestone"),
    (FEATURES, "Features"),
    (TUTORIALS, "Tutorials"),
    (CAREERS, "Careers"),
    (COMMUNITY, "Community"),
]

forms.py

class Meta:
    model = Post
    fields = [
        "title",
        # "slug",
        "category",
        "associated_portfolios",
        "body",
        # "created_on",
        # "allow_comments",
    ]
    exclude = ('allow_comments',)
    # widgets = {
    #     'symbol': autocomplete.ModelSelect2Multiple(url='symbol-autocomplete'),
    # }

def __init__(self, *args, **kwargs):
    super(PostCreateForm, self).__init__(*args, **kwargs)
    for field_name, field in self.fields.items():

        if field.widget.attrs.get("class"):
            field.widget.attrs["class"] += " form-control"
        else:
            field.widget.attrs["class"] = "form-control"
Back to Top