I have the following models:

class AccountManager(models.Model):
    account_manager = models.ForeignKey(
      settings.AUTH_USER_MODEL, 
      on_delete=models.CASCADE
    )

    def __str__(self):
        return str(self.account_manager)

class Client(models.Model):
    account_manager = models.ForeignKey(AccountManager, 
on_delete=models.CASCADE, related_name='account_manager_clients')
    client = models.CharField(max_length=255)

    def __str__(self):
        return self.client

class Contract(models.Model):
    client = models.ForeignKey(Client, on_delete=models.CASCADE, 
related_name='client_contracts')
    site_name = models.CharField(max_length=255)

    def __str__(self):
        return self.site_name

I can restrict the clients to the specific account managers by using the 
following view"
class ClientListView(LoginRequiredMixin, ListView):
    model = Client
    template_name = "clients/list.html"

    def get_queryset(self, *args, **kwargs):
        return (
            super()
            .get_queryset(*args, **kwargs)
            .filter(account_manager__account_manager=self.request.user)
        )

However I can't seem to restrict the contracts to the account manager with 
the following view:
class AMContractDetailView(LoginRequiredMixin, DetailView):
    model = Contract
    template_name = 'contracts/am_contract_detail.html'

    def get_queryset(self, *args, **kwargs):
        return (
            super()
            .get_queryset(*args, **kwargs)
            .filter(client__account_manager=self.request.user.id)
        )
This allows account managers to see all the clients not just their own.
I know I am doing something fundamentally wrong but have got a bit lost!

Any help would be appreciated.




-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7405a8f2-bdac-4e20-b53b-3f9fefb6ea27n%40googlegroups.com.

Reply via email to