Python Django Rest Framework UnorderedObjectListWarning

前端 未结 7 2123
时光取名叫无心
时光取名叫无心 2021-01-30 15:47

I upgraded from Django 1.10.4 to 1.11.1 and all of a sudden I\'m getting a ton of these messages when I run my tests:

lib/python3.5/site-packages/rest_framework/         


        
7条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 16:05

    Let me give an answer updated to new developments...

    https://code.djangoproject.com/ticket/6089

    The default ordering of the User model has been removed in Django. If you found yourself at this page because of an upgrade, it's very likely connected to this change.

    There are 2 versions of this problem you might be dealing with.

    1. your own model does not have a default ordering in its Meta (see accepted answer)
    2. you are using a model from an app you are using as a dependency which does not have a default ordering

    Since literally the Django User model itself does not adhere to ordering, it's very clear that the second scenario cannot be resolved by asking the maintainers of those dependencies to put in a default ordering. Okay, so now you either have to override the model being used for whatever your doing (sometimes a good idea, but not good for addressing such a minor issue).

    So you're left with addressing it on the view level. You also want to do something that will play nicely with any ordering filter class you have applied. For that, set the view's ordering parameter.

    class Reviewers(ListView):
        model = User
        paginate_by = 50
        ordering = ['username']
    

    Also see Is there Django List View model sort?

提交回复
热议问题