[Django]-How to check if the object has property in view in Django?

53đź‘Ť

âś…

You can use hasattr() to check to see if model has the documents property.

if hasattr(self.model, 'documents'):
    doStuff(self.model.documents)

However, this answer points out that some people feel the “easier to ask for forgiveness than permission” approach is better practice.

try:
    doStuff(self.model.documents)
except AttributeError:
    otherStuff()
👤kaezarrex

Leave a comment