206
So in order to fix this I had to find all of the all
, offset
, filter
, and limit
clauses and add a order_by
clause to them. Some I fixed by adding a default ordering:
class Meta:
ordering = ['-id']
In the ViewSets for Django Rest Framework (app/apiviews.py) I had to update all of the get_queryset
methods as adding a default ordering didn’t seem to work.
74
I was getting this warning when i used objects.all() in my view.py
profile_list = Profile.objects.all()
paginator = Paginator(profile_list, 25)
to fix this i changed my code to :
profile_list = Profile.objects.get_queryset().order_by('id')
paginator = Paginator(profile_list, 25)
- [Django]-Django error: got multiple values for keyword argument
- [Django]-Unittest Django: Mock external API, what is proper way?
- [Django]-Phpmyadmin logs out after 1440 secs
19
In my case, I had to add order_by('id')
instead of ordering
.
class IntakeCaseViewSet(viewsets.ModelViewSet):
schema = None
queryset = IntakeCase.objects.all().order_by('id')
Ordering
needs to be in the model using Class Meta (not View).
- [Django]-Execute code when Django starts ONCE only?
- [Django]-Django 2 – How to register a user using email confirmation and CBVs?
- [Django]-How do you log server errors on django sites
11
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.
- your own model does not have a default ordering in its
Meta
(see accepted answer) - 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']
- [Django]-Get the list of checkbox post in django views
- [Django]-Django Cache cache.set Not storing data
- [Django]-How do I get user IP address in Django?
5
Another option is to add OrderingFilter
http://www.django-rest-framework.org/api-guide/filtering/#orderingfilter
- [Django]-How do I run tests against a Django data migration?
- [Django]-Django models: Only permit one entry in a model?
- [Django]-Mac OS X – EnvironmentError: mysql_config not found
2
Including this didn’t work for me.
class Meta:
ordering = ['-id']
But changing get_queryset(self) and sorting the list with .order_by(‘id’) did. Maybe worked because I’m using filters, I don’t know
class MyView(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MySerializerSerializer
def get_queryset(self):
user = self.request.user
return MyModel.objects.filter(owner=user).order_by('id')
- [Django]-Django: Get model from string?
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-Django Rest Framework – Updating a foreign key
1
Update the model meta class instead.
class UsefulModel(models.Model):
class Meta:
ordering='-created' # for example
you can still override the ordering from the view attribute ‘ordering’ As advised by AlanSE previously.
class UsefulView(ListView):
ordering = ['-created']
- [Django]-How exactly do Django content types work?
- [Django]-How to resize the new uploaded images using PIL before saving?
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?
0
In my case, it expected a tuple and that tuple has to contain a comma even when you are parsing just an item in it.
class Meta:
ordering = ('-name',)
- [Django]-Django unit tests without a db
- [Django]-Django CSRF check failing with an Ajax POST request
- [Django]-Rendering a template variable as HTML