python - order by average - Django -
i trying calculate score average of "pelicula". puntuacion table formed "pelis(foreign key of pelicula) " , "user(foreign key of pelicula)" , valor(this score) models.py
class usuario(models.model): nombre = models.charfield(max_length=50) apellidos = models.charfield(max_length=50) usuario = models.charfield(max_length=50) password = models.charfield(max_length=50) email = models.emailfield() fechanacimiento = models.datefield() categorias = models.charfield(max_length=50) def __unicode__(self): return self.usuario class pelicula(models.model): titulo = models.charfield(max_length=50) anyo = models.charfield(max_length=50) actores = models.manytomanyfield('actor') resumen = models.charfield(max_length=300) categoria = models.charfield(max_length=50) puntuacionmed = models.charfield(max_length=50) director = models.foreignkey('director'); def __unicode__(self): return self.titulo class actor(models.model): nombre = models.charfield(max_length=50) apellidos = models.charfield(max_length=50) biografia = models.charfield(max_length=300) def __unicode__(self): return self.nombre class director(models.model): nombre = models.charfield(max_length=50) apellidos = models.charfield(max_length=50) biografia = models.charfield(max_length=300) def __unicode__(self): return self.nombre class puntuacion (models.model): user = models.foreignkey('usuario') pelis = models.foreignkey('pelicula') valor = models.integerfield()
the following should give answer:
from django.db import avg punctuation.objects.values('pelis_id').annotate(average=avg('valor'))
Comments
Post a Comment