Home > Blockchain >  Django-filter how to show only some objects on dropdown?
Django-filter how to show only some objects on dropdown?

Time:10-27

My site simply works like this: every Manager can have some SubManagers, those SubManagers can have some Agents (so the Agents are indirectly related to the Manager, see models.py to understand better the relations between them). I want to show in the Manager's profile page (see views.py) all the MembershipCard created by his/her related Agents. I'm trying to implement a filter to search, for example, cards created by a specific Agent, i'm able to do this but i would like to show in the dropdown only the Agents related to the Manager, the dropdown list now shows all Agents in the database

models.py

class StandardProfile(models.Model):
    name = models.CharField(max_length=200, null=True)
    surname = models.CharField(max_length=200, null=True)
    phone_number = models.CharField(max_length=200, null=True)
    class Meta:
        abstract = True

class Manager(StandardProfile):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)

class SubManager(StandardProfile):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    manager = models.ForeignKey(Capo, null=True, on_delete = models.SET_NULL)

class Agent(StandardProfile):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    manager = models.ForeignKey(Manager, null=True, on_delete = models.SET_NULL)
    subManager = models.ForeignKey(SubManager, null=True, blank=True, on_delete = models.SET_NULL)

class MembershipCard(models.Model):
    agent = models.ForeignKey(Agent, null=True,blank=True, on_delete = models.SET_NULL)
    client = models.ForeignKey(Client, null=True,blank=True, on_delete = models.SET_NULL)
    creation_date = models.DateTimeField(auto_now_add=True, null=True)
    activation_date = models.DateTimeField(null=True,blank=True)
    expiration_date = models.DateTimeField(null=True,blank=True)

views.py

@login_required(login_url='login')
def profilePage(request, pk): #www.mysite.com/profilePage/<pk>
    user = User.objects.get(id=pk) #getting the user from <pk>
    cards = MembershipCard.objects.filter(agent__manager=user.manager)
    myFilter = MembershipCardFilter(request.GET,queryset=cards,user=user)
    cards = myFilter.qs
    #page_obj is used for Pagination, and contains the cards, i removed this part of code for better readability, can add it if needed
    context = {'page_obj': page_obj,"user": user,"myFilter":myFilter}
    return render(request, 'polls/profilePage.html',context)

filters.py

class MembershipCardFilter(django_filters.FilterSet):
    class Meta:
        model = MembershipCard
        fields = ['agent','agent__subManager']
        exclude = ['creation_date']

By reading answers to similar questions i think i have to modify the __init__ method in the CardFilter class, i've tried to adapt some answers to my case but it didn't work for some reasons . Any anser/comment is appreciated! PS: I don't know if the title is clear, feel free to suggest a better one

CodePudding user response:

You can try feeding the agent dropdown during init like (not tested!):

class MembershipCardFilter(django_filters.FilterSet):

    agent= django_filters.ModelChoiceFilter(
        queryset=Agent.objects.none(),
    )

    class Meta:
        model = MembershipCard
        fields = ['agent','agent__subManager']
        exclude = ['creation_date']

    def __init__(self, *args, **kwargs):
        user = kwargs.get("user")
        agents = Agent.objects.filter(manager__user=user)
        super().__init__(*args, **kwargs)
        self.filters["agent"].queryset = agents
  • Related