bennylope / django-organizations

:couple: Multi-user accounts for Django projects

Home Page:http://django-organizations.readthedocs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to only list objects belonging to an organization

btzs opened this issue · comments

commented

Hi!
I only want to list objects belonging to the organization. How can I change the queryset of my class based view?

**models.py**
class Item(models.Model):
    # Relationships
    company = models.ForeignKey(
        'organizations.Organization',
        related_name='items',
        on_delete=models.CASCADE
    )

    # Fields
    created = models.DateTimeField(auto_now_add=True, editable=False)
    cust_item = models.TextField(max_length=100)

**views.py**
class ItemListView(generic.ListView):
    model = models.Item
    form_class = forms.ItemForm

    def get_queryset(self):
        return models.Item.objects.all()

I think this is covered in the documentation, but unfortunately I cannot get it to work:

You can simplify access checks with a queryset method.:

class ProductQuerySet(models.QuerySet):
    def for_user(self, user):
        return self.filter(account__users=user)

Views & mixins

The views in the rest of your application can then filter by account.:

class OrganizationMixin(object):
commented

Sorry I missed a similar issue. #137 (comment)
I will try that first