filter in queryset

30 views
Skip to first unread message

Tony Peña

unread,
Sep 12, 2015, 12:54:59 PM9/12/15
to django...@googlegroups.com
Hi

need a help with this queryset problem
i got into my models.


models.py
---------

class Classi(models.Model):
    classi = models.CharField('Class Name', max_length='20', unique=True)
    internet = models.BooleanField(default=None)

using Calssi can do:
create: A, B. C

class Prof(models.Model):
    prof = models.ForeignKey(User)
    classi = models.ManyToManyField(Classi)

i can create a simple User: teacher, and can belong to A, C  Classi

class MAC(models.Model):
    classi = models.ForeignKey(Classi)
    name = models.CharField('dev_name', max_length=64, default='unnamed', null=True, blank=True)
    mac = MACAddressField('MAC', blank=False, unique=True)
    internet = models.BooleanField(default=None)

i create simple macs objects where those belongs to Classi

after that i got

admin.py
--------

class ClassAdmin(admin.ModelAdmin):
 def get_queryset(self, request):
        qs = super(ClassAdmin, self).get_queryset(request)
        if request.user.is_staff and request.user.is_superuser:
            return qs
        return qs.filter(prof__prof=request.user) 

with this I can see only Classi A and B if I logged as User with A and B classrooms set it and that's ok

but:
--------------------------------------------
i'm testing 4 macs:  Class A: 1mac ,   Class B: 2mac,   Class C: 1mac

when i define:

class MacAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(MACAdmin, self).get_queryset(request)
        if request.user.is_staff and request.user.is_superuser:
            return qs
        return qs.filter(classi_id__mac=request.user)

for make a test I create an user belogs to Class A and B so would be see 3 macs on list_display
but only got 1

on debug mode, qs variable i can see the 4 macs,   i need to know filter it to get only that A and B because is filtered by user wich only belogs to A and B class

thanxs

--
Antonio Peña
Secure email with PGP 0x8B021001 available at https://pgp.mit.edu
Fingerprint: 74E6 2974 B090 366D CE71  7BB2 6476 FA09 8B02 1001

James Schneider

unread,
Sep 13, 2015, 4:28:34 AM9/13/15
to django...@googlegroups.com
but:
--------------------------------------------
i'm testing 4 macs:  Class A: 1mac ,   Class B: 2mac,   Class C: 1mac

when i define:

class MacAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(MACAdmin, self).get_queryset(request)
        if request.user.is_staff and request.user.is_superuser:
            return qs
        return qs.filter(classi_id__mac=request.user)

for make a test I create an user belogs to Class A and B so would be see 3 macs on list_display
but only got 1

on debug mode, qs variable i can see the 4 macs,   i need to know filter it to get only that A and B because is filtered by user wich only belogs to A and B class

thanxs


Not sure if I'm following you 100%, but i think you can filter like this:

return qs.filter(classi__prof=request.user)

This would match the MAC to a class, and the class to a professor, and the professor to request.user. Should return all of the MAC's that belong to a class that request.user is a professor for, I believe.

-James

James Schneider

unread,
Sep 13, 2015, 5:19:57 AM9/13/15
to django...@googlegroups.com
Not sure if I'm following you 100%, but i think you can filter like this:

return qs.filter(classi__prof=request.user)

This would match the MAC to a class, and the class to a professor, and the professor to request.user. Should return all of the MAC's that belong to a class that request.user is a professor for, I believe.


I was thinking on this, you might have to use qs.filter(classi__prof__prof=request.user) instead.

-James
 

Tony Peña

unread,
Sep 13, 2015, 7:51:13 AM9/13/15
to django...@googlegroups.com
thanxs so much... that works perfect.!

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciU5sVtPGvJsi66xBPxwow_ctQ3P-vrGkgm-JoEPNTQJYg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages