Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable importing authtoken.models when using SessionAuthentication with django-rest-framework?

In my rest_framework settings I set SessionAuthentication as the default authentication class

'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.SessionAuthentication',
)

The problem is that my User DELETE api view tries to collapse delete on the authtoken_token table, which doesn't exist (since I don't need it), raising a database exception.

Specifically, when obj.delete() is invoked in DestroyModelMixin.destroy, this exception is raised:

DatabaseError: relation "authtoken_token" does not exist
LINE 1: ...oken"."user_id", "authtoken_token"."created" FROM "authtoken...

Is one required to syncdb rest_framework's models, even if one doesn't need them?

Is there a way to use SessionAuthentication without including authtoken.models?

Am I doing something wrong?

(removing authtoken.models.Token completely from rest_framework/authentication.py seems to solve the problem)

like image 914
Nejc Jelovčan Avatar asked Dec 14 '25 15:12

Nejc Jelovčan


1 Answers

Here's the root cause...

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

Will sort out a workaround in the meantime, which will probably just involve making sure Token doesn't get imported anywhere unless rest_framework.authtoken is in INSTALLED_APPS.

like image 150
Tom Christie Avatar answered Dec 17 '25 09:12

Tom Christie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!