0

I have two models:

class ModManager(models.Manager):
    def myfilter(self, options = dict()):

    if options.has_key('not_image'):
        kwargs['image__isnull'] = False

    return self.filter(**kwargs)

class Model_1(models.Model):
    ...
    objects = MyManager()

class Model_2(models.Model):
    something = models.ForeignKey(Model_1)
    something_else = ...
    ...

How to get all data from Model_2 related to Model_1 in MyManager? I want to get one query. I have so far:

in Model_1:

def get_model_2(self):
    self.model_2_objs = self.model_2_set.all()

But it generates many queries when I calling get_model_2 function.

Nips
  • 13,162
  • 23
  • 65
  • 103
  • 1
    Never do this: `def myfilter(self, options = dict())`. [#5 here](http://zephyrfalcon.org/labs/python_pitfalls.html) – DrTyrsa Feb 22 '12 at 08:41
  • In myfiler I only check options, not declare anything. – Nips Feb 22 '12 at 08:59
  • Ok, no one can prevent you from using antipatterns like this. Sometimes it's better to learn from mistakes. – DrTyrsa Feb 22 '12 at 09:05
  • 1
    Use itertools.chain, see http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view – James R Feb 22 '12 at 12:50

0 Answers0