I am new with django and I am attempting to make one test where I wish to compare a QuerySet pre and post a load editing function call.

    def test_batchEditing_9(self):
      reset() #reset database for test
      query       = Game.objects.all()
      query_old   = Game.objects.all()
      dict_value  = {'game_code' : '001'}
      Utility.batchEditing(Game, query, dict_value)
      query_new   = Game.objects.all()
      self.assertTrue(compareQuerySet(query_old, query_new))

My issue is that question_old is going to be up-to-date after batchEditing is known as. Because of this, both querysets would be the same.

It appear that QuerySet is certain to the present condition from the database. Is normal? It is possible to method to unbind a QuerySet in the database?

I've attempted queryset.values, list(queryset) however it still updates the worthiness. I am really considering iterating around the queryset and creating a listing of dictionaries on my own, but I wish to determine if there's an simpler way.

Here's batchEditing (did not paste input validity check)

def batchEditing(model, query, values):
      for item in query:
        if isinstance(item, model):
          for field, val in values.iteritems():
             if val is not None:
                setattr(item, field, val)
        item.save()

Here's compareQuerySet

def compareQuerySet(object1, object2):
  list_val1 = object1.values_list()
  list_val2 = object2.values_list()
  for i in range(len(list_val1)):
    if list_val1[i] != list_val2[i]:
        return False
  return True

A Queryset is basically just producing SQL and just on evaluating it, the database is hit. So far as I recall, that occurs on iterating within the Queryset. For example,

gamescache = list(Game.objects.all())

or

for g in Game.objects.all():
    ...

hit the database.

Following code should work:

def test_batchEditing_9(self):
  reset() #reset database for test
  query       = Game.objects.all()
  query_old   = set(query)
  dict_value  = {'game_code' : '001'}
  Utility.batchEditing(Game, query, dict_value)
  query_new   = set(query)
  self.assertEqual(query_old, query_new)

The reason being Game.objects.all() isn't striking database, but simply produces object that stores query parameters.

BTW. For a moment use order_by in query and order is essential you should use list instead of set.