Django queryset more simpel

34 Views Asked by At

is there a way to combine those 2 lines of code?

a = terminal.provisioning_set.first()
b = a.usergroup.id

the result is coming from:

 terminal = Terminal.get_object(terminal_id, request.user)

I like to see something like this:

result = terminal.provisioning_set.first(usergroup.id)
1

There are 1 best solutions below

0
Ram On

first() doesn't accept any arguments. Docs

first() - Returns the first object matched by the queryset, or None if there is no matching object.

So what you've shown is incorrect.

You can try like this.

result = terminal.provisioning_set.first().usergroup.id