Skip to content

Instantly share code, notes, and snippets.

@brechin
Created September 21, 2018 14:56
Show Gist options
  • Save brechin/6bab90799d3cb3491de9c19fb8b77cb4 to your computer and use it in GitHub Desktop.
Save brechin/6bab90799d3cb3491de9c19fb8b77cb4 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.db import models
import computed_property
class MyModel(models.Model):
doubled = computed_property.ComputedIntegerField(compute_from='double_it')
base = models.IntegerField()
count = computed_property.ComputedIntegerField('count_related')
def double_it(self):
return self.base * 2
@property
def count_related(self):
print 'counting!'
return AnotherModel.objects.filter(related=self).count()
def as_dict(self):
return {
'id': self.id,
'doubled': self.doubled,
'count': self.count,
'base': self.base
}
class AnotherModel(models.Model):
related = models.ForeignKey('MyModel', null=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment