Tag Archives: drf

DRF uses paging function Error: unorderedobjectlistwarning

UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list: <class ‘myapp.models.Power’> QuerySet.
paginator = self.django_paginator_class(queryset, page_size)

The reason is that you are not sorting.

Solution.
(a) add ordering to the design of the model

class test(models.Model):
    ...

    class Meta:
        ...
        ordering=["xx"]

(b) Add order_by to the query set for sorting

Djangorestframework-simplejwt: ‘str‘ object has no attribute ‘decode‘ [Solved]

Problem description

Python v3.6.6Django v3.2.4djangorestframework v3.12.4djangorestframework-simplejwt v4.4.0

When testing the interface after running runserver command, the error of background printing is as follows

Traceback (most recent call last):
  File "D:\Program Files\Python36\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "D:\Program Files\Python36\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\Program Files\Python36\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "D:\Program Files\Python36\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "D:\Program Files\Python36\lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework_simplejwt\views.py", line 27, in post
    serializer.is_valid(raise_exception=True)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
    self._validated_data = self.run_validation(self.initial_data)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework\serializers.py", line 422, in run_validation
    value = self.validate(value)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework_simplejwt\serializers.py", line 75, in validate
    data['refresh'] = str(refresh)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework_simplejwt\tokens.py", line 82, in __str__
    return token_backend.encode(self.payload)
  File "D:\Program Files\Python36\lib\site-packages\rest_framework_simplejwt\backends.py", line 43, in encode
    return token.decode('utf-8')
AttributeError: 'str' object has no attribute 'decode'

terms of settlement

Method 1

Upgrade the djangorestframework simplejwt version to 4.6.0 +

Method 2

Open
D:// program files/Python 36/lib/site packages/rest_ framework_ simplejwt\backends.py

File, edit line 43. take

return token.decode('utf-8')

It can be amended as follows

return token

explain

Migration admin.0001_initial is applied before its dependency xxx.0001_initial on database ‘default‘

Problem orientation

Developed in Django framework, a user table is defined to replace the user table automatically generated by the framework, which appears when the migrate command is executed to synchronize the database. Details of the error are as follows

E:\SweetYaya\MyProj01> python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "D:\Program Files\Python36\lib\site-packages\django\core\management\commands\migrate.py", line 95, in handle
    executor.loader.check_consistent_history(connection)
  File "D:\Program Files\Python36\lib\site-packages\django\db\migrations\loader.py", line 310, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency MyApp.0001_initial on database 'default'.

terms of settlement

Method 1

delete database , delete all Auth in the database_ and Django_ and re execute it. It's rough. It's not good.

Method 2

First makemigrations Open settings. Py and comment out install_ In apps,
'django. Contrib. Admin ', Open URLs. Py , comment out the admin in urlpatterns, and then migrate will not report an error. Finally, pay attention to recover the comment content