def check_custom_image_filter_for_modelAdmin(self, modelAdmin): """ The conditions for the image to be on the query are pics with the is_used=False(default) I am stubbing the resquest and the db_field """ request = {} class foo(object): name = "pic" def formfield(self, **kwargs): self.args = kwargs db_field = foo() modelAdmin.formfield_for_foreignkey(db_field, request) # So far so good queryset = db_field.args["queryset"] #no images found self.assertEqual(queryset.count(),0) # Now I create 3 files to the 3 conditions to see if the query finds only two f = File(open("core/tests/test_images/test.jpg", "r")) p1 = Photo() p1.title = "test1" p1.image.save("testfile1.jpg", f) p1.save() f = File(open("core/tests/test_images/test.jpg", "r")) p2 = Photo() p2.title = "test2" p2.image.save("testfile2.jpg", f) # this should not apear p2.used = True p2.save() f = File(open("core/tests/test_images/test.jpg", "r")) p3 = Photo() p3.title = "test3" p3.image.save("testfile3.jpg", f) p3.used = True p3.save() Post.objects.create(title="foo",slug="foo",text="foo",pic=p3) #Finding only p1 and p3 self.assertEqual(queryset.count(),2) p1.delete() p2.delete() p3.delete()