Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Created May 30, 2014 13:21
Show Gist options
  • Save hirokazumiyaji/db4557760041b3a67830 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/db4557760041b3a67830 to your computer and use it in GitHub Desktop.
class Smaple(models.Model):
var_id = models.IntegerField()
@cached_property
def var(self):
return Var.get(self.var_id)
class Var(models.Model):
@classmethod
def get(cls, pk):
return cls.objects.get(pk=pk)
# pattern1
vars = []
for sample in Sample.objects.all():
l.appned(sample.var)
# petterns
var_ids = [sample.var_id for sample in Sample.objects.all()]
vars = Var.objects.in_builk(var_ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment