Django 1.9, Python 3.6, postgres DB
There exists Calendar
and CalendarOwner
, where the many-to-many relationship is defined in Calendar
.
class Calendar(models.Model):
...
calendar_owners = models.ManyToManyField(
'some_app.CalendarOwner',
blank=True,
related_name='calendars',
)
...
def calendar_method(self):
return self.calendar_owners.calendar_owner_method()
class CalendarOwner(models.Model):
...
def calendar_owner_method(self):
...
# returns a bool, depends on condition
When I am calling Calendar.calendar_method()
for Calendar
that has CalendarOwner
(s) associated with it, CalendarOwner.calendar_owner_method()
is called for each CalendarOwner
, and works as intended - I get a boolean according to whatever logic I have in there.
I think that the way it works is that from all the method calls, if there is at least one True
the execution breaks and the return value is True
.
Otherwise it will call on all the related objects and finally return False
.
The question:
Why is it that when I am calling Calendar.calendar_method()
when no CalendarOwner
(s) are associated with Calendar
it never calls CalendarOwner.calendar_owner_method()
and always returns True
.
Is that the default behavior?
Example:
>>> obj = Calendar()
<Calendar: Str>
>>> obj.calendar_owners.count()
0
>>> obj.calendar_owners.calendar_owner_method()
True
It makes sense that since there are no CalendarOwner
(s) associated with that Calendar
the CalendarOwners.calendar_owner_method()
would not be called at all. But why does it return True? Can someone point me to the docs?
I can check if Calendar.calendar_owners.count()
is zero and return False
, but I want to see if there is any better Django way of doing that.