Compare logged-in user against record owner in filter

4 views
Skip to first unread message

Clive Bruton

unread,
Jun 10, 2020, 11:43:19 AM6/10/20
to django...@googlegroups.com
I am trying to create a queryset that filters against the logged-in
user's id and the user_id in the record, ie so that only the relevant
user can see the record.

Without the filter, when I display the page I can capture elements in
the template that would allow this comparison, ie:

object.user.id # the record owner
user.id # the logged-in user's id
object.user # the record owner's user name
user # the logged-in user’s user name

So, in models.py I made a new query manager:

class EditManager(models.Manager):
def get_queryset(self):
return super(EditManager, self).get_queryset().filter(
user.id==object.user.id,
expires__gte=datetime.now(timezone.utc),
is_active=True)

Then the debug gives the following error:

Exception Value:name 'user' is not defined

Even if I try fields that are obviously in the table, just to get a
nonsensical filter, I get similar errors, eg "id==user_id".

I have tried variations on the user id comparison, but have not got
any further.

Or, am I going about this completely the wrong way?

Any help greatly appreciated.


-- Clive
Reply all
Reply to author
Forward
0 new messages