I know people on SO/SE are a bit butthurt that no one knows where to asks the questions, so I hope this thread somehow fits into this sub forum ;-)
If i have to check if a value in my django model is set (!= None) i do it like this:
if not model.fiel:
model.field = some_logic()
It feels pretty pythonic but i honestly don't know that this if statement does. I assume it check if the Value is True/False? If the Value is 0 or an empty iterable ('', {}, () or []) it will also fail.
I find my self using this alot because its pretty convenient. Could someone explain to me what it does or why i shouldn't (or should) use it?
Edit (add example):
#imports
from apptime.models import Incident
from django.utils import timezone
import requests
#part of logic
#django queryset (list like object filled with DB models
incidents = Incident.object.filter(monitor=client)
#check if queryset is not empty;
if incidents:
#get latest
incident = incidents.latest('start_downtime')
#check if fk model has datetime field set
if not incident.monitor.down_since:
#set datetime field if not jet set
incident.monitor.down_since = timezone.now()
I think that the if
call to a variable calls the bool()
built-in function. At least it acts exactly like it, so I'll just continue to use it.