Home:ALL Converter>How to declare and call a model method navigating through relations in django?

How to declare and call a model method navigating through relations in django?

Ask Time:2013-07-27T19:33:30         Author:Pol Alvarez Vecino

Json Formatter

I Have 3 models

class Room(models.Model):

    house = models.ForeignKey('Property', related_name='rooms')
    beds = models.IntegerField(max_length=10)

class Property(models.Model):

......

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


class Profile(model.Models):

    room = models.ForeignKey(Room, blank=True, null=True, related_name='tenant')

First of all I would like to know if free_places method should be: free_places(self) or free_places(). I think it has some errors too but I don't know how to test it, so here it's the question how can I call the method? and is it correct the function? I want to call it from a django template.

To try it from "./manage.py shell" what do I need?

Thanks

Author:Pol Alvarez Vecino,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/17897286/how-to-declare-and-call-a-model-method-navigating-through-relations-in-django
yy