[Answer]-How to declare and call a model method navigating through relations in django?

1👍

It should be free_places(self):

You can define the method as a property and then call that from the template:

https://docs.djangoproject.com/en/dev/topics/db/models/#model-methods

Last example in that code block

def _free_places(self):
    places = 0
    for r in self.rooms:
        x = r.beds - ((r.tenant).size())
        places = places + x
    return places
free_places = property(_free_places)

Leave a comment