Utkarsh Sinha
Utkarsh Sinha

Reputation: 3305

Test if a foreign key is None or not

I have a model in django that points to another model. I want to test if it's set to something or not.

model.foreignkey = ForeignKeyField(...)

I want to test if this foreignkey is set or not - without actually fetching the foreign row's data (if the foreign key is set).

if model.foreignkey: # This line fetches data for 
    <dosomething>

Any help?

Upvotes: 1

Views: 957

Answers (1)

danielcorreia
danielcorreia

Reputation: 2136

Test for model.foreignkey_id

Check out the docs: https://docs.djangoproject.com/en/dev/ref/models/fields/#database-representation

Upvotes: 4

Related Questions