我有一个模型老师,该老师教具有“等级”属性的课程。每次上完课后,班上的每个学生都可以评论和查看他的表现。他们通过创建OccurrenceRating的新对象实例来实现。如何将OccurrenceRating实例中的每个新“评级”保存到教师实例“评级”,并确保平均值正确。通过获取与该老师的所有OcccurrenceRating对象并获得该老师的平均值,我已经能够显示每位老师的平均值,但是我无法保存该值。我该怎么做?
Models.py
教师模型
class ProfileTeacher(models.Model):
created = models.DateTimeField(auto_now=False, auto_now_add=True, blank = False, null = False, verbose_name = 'Creation Date')
user = models.OneToOneField(app_settings.USER_MODEL,blank=True, null=False)
first_name = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'First Name')
last_name = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Surname')
phone_number = models.CharField(max_length = 15, null=True, blank = True, verbose_name = 'Phone Number')
city = models.ForeignKey(City, null=True, blank = True, verbose_name = 'City')
postal_code = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Postal Code')
adress = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'Address')
nationality = CountryField(blank_label='(select country)', null=True, blank = True)
id_number = models.CharField(max_length = 400, null=True, blank = True, verbose_name = 'ID Number(NIF/NIE)')
bank_account = IBANField(verbose_name='IBAN', blank = True, null=True)
date_of_birth = models.DateField(auto_now=False, auto_now_add=False, blank = True, null = True, verbose_name='Date of birth')
#To enable them showing up in search. Only active teachers are shown and preapproved needs to be true to be active
preapproved = models.BooleanField(default=False, blank=True, verbose_name='Pre-Approved')
active = models.BooleanField(default=False, blank=True, verbose_name='Active')
#Braintree and Milingual Terms of Service Acceptance
tos = models.BooleanField(default=False, blank=True)
#If true will receive classes email notifications
email_notification = models.BooleanField(default=False, blank=True, verbose_name="Recibir Notificaciones")
GENDER = (
('male','Male'),
('female','Female'),
)
gender = models.CharField(max_length=20, choices=GENDER, blank = True, null=True, verbose_name = 'Gender' )
language = models.ForeignKey(Language, null = True, blank=True)
teacher_type = models.ForeignKey(TeacherType, null = True, blank=True, verbose_name='Type')
cvv = models.FileField(upload_to=teacher_cv_directory_path, null=True, blank = True, verbose_name="CV")
profile_image = models.ImageField(upload_to=teacher_avatar_directory_path,
null = True,
blank=True,
default='/perfil.png',
)
RATINGS = (
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)
rating = models.DecimalField(max_digits=3, decimal_places=2,default=0, choices=RATINGS, blank=True, verbose_name="RATINGS", null=False)
#True if the profile will appear on milingual site as Public
published = models.BooleanField(null=False, blank=False, default=True)
#Url: Teacher's Url
url = models.SlugField(max_length=400, null=True, blank = True, verbose_name='Url')
# True if teacher is legacy
legacy = models.BooleanField(default=False, blank=True)
legacy_id = models.IntegerField(default=0, null=True, blank=True)
class Meta:
verbose_name_plural = 'Teachers'
verbose_name = 'Teacher'
@property
def absolute_rating(self):
return int(float(self.rating)/5 * 100)
def save(self, *args, **kwargs):
# Check how the current values differ from ._loaded_values. For example,
# prevent changing the creator_id of the model. (This example doesn't
# support cases where 'creator_id' is deferred).
#Building_unique_url
url = str(self.first_name).lower() + '_' + str(self.last_name).lower() + '_' + str(int(self.user.id))
self.url = url
if self.rating == '':
self.rating = 0
if self.profile_image!='/perfil.png'and self.cvv!='':
self.preapproved = True
else:
self.preapproved = False
try:
this = ProfileTeacher.objects.get(id=self.id)
if this.profile_image != self.profile_image:
this.profile_image.delete(save=False)
except: pass # when new photo then we do nothing, normal case
super(ProfileTeacher, self).save(*args, **kwargs)
def __unicode__(self):
return "%s %s" % (self.first_name, self.last_name)
Class OccurrenceRate Model
class OccurrenceRating(models.Model):
# Update
updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank = False, null = False, verbose_name = 'Updated')
# Created
created = models.DateTimeField(auto_now=False, auto_now_add=True, blank = False, null = False, verbose_name = 'Creation Date')
# Author
student = models.ForeignKey(ProfileStudent, verbose_name = 'Student')
# Rating Value: 1 - 5
CHOICHES =(
(1, '1'),
(2, '2'),
(3, '3'),
(4, '4'),
(5, '5'),
)
occurrence = models.ForeignKey(Occurrence, verbose_name = 'Class')
# Teacher Rating
tc_rating = models.IntegerField(choices=CHOICHES, null=True, blank = True, verbose_name = 'Evaluación Profesor')
# Feedback to teacher
tc_comment = models.TextField(max_length = 5000, null=True, blank = False, verbose_name = 'Comentario al profesor')
# Feedback to teacher
vn_rating = models.IntegerField(choices=CHOICHES, null=True, blank = True, verbose_name = 'Evaluación Local')
# Occurrence comment
vn_comment = models.TextField(max_length = 5000, null=True, blank = False, verbose_name = 'Comentario al Local')
# Occurrence Rated
答案 0 :(得分:1)
您可以在学生对该教师评分的任何新实例上更新Teacher
实例评分。
示例:
# req_query_teacher_id - I am assuming it is from the query params in request
teacher = Teacher.objects.get(id=req_query_teacher_id)
oc = OccurrenceRating.objects.create(...)
# get average rating for this teacher
avg_rating = get_avg_rating(teacher)
teacher.rating = avg_rating
teacher.save()
无论何时创建新的学生评分实例,都要考虑当时存在的所有评分来更新教师的平均评分。