How do I test Django QuerySets are equal?

前端 未结 6 753
挽巷
挽巷 2020-12-17 07:40

I am trying to test my Django views. This view passes a QuerySet to the template:

def merchant_home(request, slug):
  merchant = Merchant.objects.get(slug=sl         


        
6条回答
  •  温柔的废话
    2020-12-17 08:31

    By default assertQuerysetEqual uses repr() on the first argument. This is why you were having issues with the strings in the queryset comparison.

    To work around this you can override the transform argument with a lambda function that doesn't use repr():

    self.assertQuerysetEqual(queryset_1, queryset_2, transform=lambda x: x)
    

提交回复
热议问题