Exemple #1
0
    def rel_f(instances, filter_f):
        instances = deferred_to_real(instances)
        c = {}
        c['%s__in' % to_model_field] = [
            getattr(instance, from_model_fk_field) for instance in instances
        ]
        q = to_model.objects.filter(**c)
        if to_model_field not in ['pk', 'id']:
            f = {}
            f[to_model_field] = to_model_field
            q = q.extra(select=f)
        q = filter_f(q)

        to_from = {}

        for instance in instances:
            k = getattr(instance, from_model_fk_field)
            if k not in to_from:
                to_from[k] = []
            to_from[k].append(instance)

        r = []
        for obj in q:
            for src in to_from[getattr(obj, to_model_field)]:
                r.append((obj, src.pk))
        return r
Exemple #2
0
 def test_conversion_uses_single_query(self):
   # We have to prefix with .all() to prevent the object cache from returning complete
   # objects from previous queries
   entries = list(Entry.objects.all().filter(blog__name='Databases').only('id'))
   with override_settings(DEBUG=True):
     entries = list(deferred_to_real(entries))
     self.assertEquals(len(connection.queries) - self.query_count, 1)
Exemple #3
0
  def rel_f(instances, filter_f):
    instances = deferred_to_real(instances)

    c = {}
    c['%s__in' % to_model_field] = [getattr(instance, from_model_fk_field) for instance in instances]
    q = to_model.objects.filter(**c)
    if to_model_field not in ['pk', 'id']:
      f = {}
      f[to_model_field] = to_model_field
      q = q.extra(select=f)
    q = filter_f(q)

    to_from = {}

    for instance in instances:
      k = getattr(instance, from_model_fk_field)
      if k not in to_from:
        to_from[k] = []
      to_from[k].append(instance)

    r = []
    for obj in q:
      for src in to_from[getattr(obj, to_model_field)]:
        r.append((obj, src.pk))
    return r
Exemple #4
0
 def test_conversion_uses_single_query(self):
     # We have to prefix with .all() to prevent the object cache from returning complete
     # objects from previous queries
     entries = list(
         Entry.objects.all().filter(blog__name='Databases').only('id'))
     with override_settings(DEBUG=True):
         entries = list(deferred_to_real(entries))
         self.assertEquals(len(connection.queries) - self.query_count, 1)
Exemple #5
0
  def test_converts_deferred_objects_to_real_objects(self):
    entries = list(Entry.objects.all().filter(blog__name='Databases').only('id'))
    self.assertEquals(len(entries), 3)
    # test objects are deferred
    for entry in entries:
      self.assertEquals('id' in entry.__dict__, True)
      self.assertEquals('headline' in entry.__dict__, False)

    # convert to real
    entries = deferred_to_real(entries)
    self.assertEquals(len(entries), 3)
    for entry in entries:
      self.assertEquals('id' in entry.__dict__, True)
      self.assertEquals('headline' in entry.__dict__, True)
Exemple #6
0
    def test_converts_deferred_objects_to_real_objects(self):
        entries = list(
            Entry.objects.all().filter(blog__name='Databases').only('id'))
        self.assertEquals(len(entries), 3)
        # test objects are deferred
        for entry in entries:
            self.assertEquals('id' in entry.__dict__, True)
            self.assertEquals('headline' in entry.__dict__, False)

        # convert to real
        entries = deferred_to_real(entries)
        self.assertEquals(len(entries), 3)
        for entry in entries:
            self.assertEquals('id' in entry.__dict__, True)
            self.assertEquals('headline' in entry.__dict__, True)
Exemple #7
0
  def test_converts_mixture_of_deferred_and_real_objects(self):
    real_entries = list(Entry.objects.all().filter(blog__name='Databases'))
    self.assertEquals(len(real_entries), 3)
    # test objects are real
    for entry in real_entries:
      self.assertEquals('id' in entry.__dict__, True)
      self.assertEquals('headline' in entry.__dict__, True)

    deferred_entries = list(Entry.objects.all().filter(blog__name='Databases').only('id'))
    self.assertEquals(len(deferred_entries), 3)
    # test objects are deferred
    for entry in deferred_entries:
      self.assertEquals('id' in entry.__dict__, True)
      self.assertEquals('headline' in entry.__dict__, False)

    # convert to real and uniquefy
    entries = deferred_to_real(real_entries+deferred_entries)
    self.assertEquals(len(entries), 3)
    for entry in entries:
      self.assertEquals('id' in entry.__dict__, True)
      self.assertEquals('headline' in entry.__dict__, True)
Exemple #8
0
    def test_converts_mixture_of_deferred_and_real_objects(self):
        real_entries = list(Entry.objects.all().filter(blog__name='Databases'))
        self.assertEquals(len(real_entries), 3)
        # test objects are real
        for entry in real_entries:
            self.assertEquals('id' in entry.__dict__, True)
            self.assertEquals('headline' in entry.__dict__, True)

        deferred_entries = list(
            Entry.objects.all().filter(blog__name='Databases').only('id'))
        self.assertEquals(len(deferred_entries), 3)
        # test objects are deferred
        for entry in deferred_entries:
            self.assertEquals('id' in entry.__dict__, True)
            self.assertEquals('headline' in entry.__dict__, False)

        # convert to real and uniquefy
        entries = deferred_to_real(real_entries + deferred_entries)
        self.assertEquals(len(entries), 3)
        for entry in entries:
            self.assertEquals('id' in entry.__dict__, True)
            self.assertEquals('headline' in entry.__dict__, True)