Skip to content

How to check the PK of a foreign-key relationship without loading the related model; how to compare two objects #1658

Answered by Abdeldjalil-H
merlinz01 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @merlinz01. I know this is late, but in case you still didn't find an answer and for other poeple using Tortoise orm.
When you use a foreign key field called fk_field, tortoise will use fk_field_id to store the value of the related model id. The fk_field is used to load the relation.
For you're question, is_admin does not load the organization. You need to use fetch_related to load it. But you can check if the foreign key is null without loading it. Here's how you can do it:

@property
def is_admin(self):
    return self.organization_id is None

Same thing for your second question.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@merlinz01
Comment options

Answer selected by merlinz01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants