[Python] How to get average from set of objects in Django?

Discussão em 'Python' iniciado por Stack, Fevereiro 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple rating system for a property. You give it a mark out of 5 (stars). The models are defined like this

    def Property(models.Model)
    # stuff here

    def Rating(models.Model)
    property = models.ForeignKey(Property)
    stars = models.IntegerField()


    What I want to do is get a property, find all the Rating objects, collect them, then get the average 'stars' from them.

    Any ideas how to do this?

    Continue reading...

Compartilhe esta Página