我正在尝试计算" Pelicula"的平均得分。 Puntuacion表由pelis(Pelicula的外键)" ,"用户(Pelicula的外键)"和勇气(这是得分) 这是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()
答案 0 :(得分:1)
以下内容应该给你答案:
from django.db import Avg
Punctuation.objects.values('pelis_id').annotate(average=Avg('valor'))