Created
September 21, 2018 14:56
-
-
Save brechin/6bab90799d3cb3491de9c19fb8b77cb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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