0

I’m looking at code which has:

queryset = MyModel.objects.all()
get_queryset(self, *args, **kwargs):
    return (super().get_queryset(*args, **kargs).order_by(self.name))

What is the purpose of the args and kwargs here and why not do this instead:

get_queryset(self):
    return MyModel.objects.order_by(self.name))```
Baz
  • 12,713
  • 38
  • 145
  • 268
  • You can look at this comment here: https://stackoverflow.com/a/3394902/7479217 In your case, the get_queryset might accept other arguments you don't know about. So you just pass in *args and **kwargs to tell it to accept anything thrown at you, and pass it to the super().get_queryset function. – Mazhar Ali Apr 06 '23 at 13:41

1 Answers1

1

It is good practice to include by default anything that returns a function with the same name in parent's class, especially, if you have no idea what they do.

In example, get_queryset looks at model that is stored in an attribute of the class. If you hardcode that model it might create some confusion later if your view grows. You can have later much more functions that will simply look at that model in the attribute. Then if you need to change the model - you will change it only in one place.

Although the parenthesis seems redundant, it should work like this:

get_queryset(self, *args, **kwargs):
    return super().get_queryset(*args, **kargs).order_by(self.name)
NixonSparrow
  • 6,130
  • 1
  • 6
  • 18