Форум сайта python.su
Имеются модели:
class Item(models.Model):
name = models.CharField(_('Name'), max_length=200)
points = models.IntegerField(_('Points'), default=0)
cluster = models.ForeignKey(Cluster, verbose_name=_('Cluster'))
costumerservice = models.BooleanField(_('Customer Service'), blank=True, default=False)
class Meta:
verbose_name = _('Item')
verbose_name_plural = _('Items')
def __unicode__(self):
return u'%s (%d points)' % (self.name, self.points)
class Report(models.Model):
user = models.ForeignKey(User)
date = models.DateTimeField(_('Date'), default=datetime.now())
country = models.ForeignKey(Country)
publication = models.BooleanField(_('Publication'), blank=True, default=False)
total = models.IntegerField(default=0)
class Meta:
verbose_name = _('Report')
verbose_name_plural = _('Reports')
def cluster_name(self):
return self.country.cluster
def __unicode__(self):
return self.country.country
def save(self, *args, **kwargs):
self.total = 0
for report_item in ReportItem.objects.filter(report=self):
self.total += report_item.item.points * report_item.total
reports = Report.objects.filter(country=self.country, publication=True).all()
super(Report, self).save(*args, **kwargs)
class ReportItem(models.Model):
item = models.ForeignKey(Item)
total = models.IntegerField(default=0)
report = models.ForeignKey(Report, related_name='items', verbose_name=_('Report Item'))
Офлайн