I'm developing an app in Django which uses an existing database with created users.
I set my database configuration parameters to a PostgreSQL server and I perform my custom queries through "connections" library.
The problem comes when I want to use my own table to authenticate users. I saw many tutorials and blog posts and I rewritten my authentication backend. But when I want to use my own table to authenticate users and set sessions, Django's Framework only allows me to use User object.
I think these object is linked to Django tables in database and when I want to authenticate an user shows me a message saying the relation "auth_user" doesn't exists. This means that User object is linked to this table.
Does there, exist some method to use my own table with Django Authentication Backend or should I use it?
To use custom Django model with existing Django login backend
AUTH_USER_MODEL = 'myapp.MyUser'
Django docs
To use custom Django authentication backend
AUTHENTICATION_BACKENDS = 'myBackend'
Django docs
Related
I have working Django project. Now i want to add FastAPI so at existing django templates i can make API requests to refresh data without reloading whole template (like tables for example).
The question is how to connect FastAPI to existing django authentication system, so it can use sessions from db, so that user authenticate only once, when logging in to django project.
Here some options i have investigated:
Change django base authentication to oauth2, also set fastapi to oauth2 (though i desire to configure fastapi, not changing django)
Fetch django sessions from db on each api request and verify user
Both django and fastapi on the same server and can share the same db.
Maybe some better options are possible. Please advice what would be the best approach to use django already authenticated user data with fastapi? Thx
When I added rest_framework.authtoken and used Token.objects.get_or_create(user = user) for check auth call API, django request must create table authtoken_token in DB, but customer don't want add new table in DB.
So, have any way resolve it?
Thanks for your time!!!
Django REST Framework needs to create the table to store the access tokens if you're using the built in token authentication.
As mentioned in the comments, there are other options, including JWT. The library recommended by the DRF documentation is this one:
https://github.com/davesque/django-rest-framework-simplejwt
So I'm starting a new Django project that essentially requires the login & registration process be routed through an EXTERNAL & ALREADY created database.
Is it possible to have the User model use an EXTERNAL database table ONLY when Django is:
Logging in a user, to check if the login is valid
Registering a user, inserting data for that user in the external database
I would like for the rest of the Django server to use a local database.
If so, could someone either provide examples or guide me to documentation on the subject?
Easiest way to use multiple database with Django is to use a database routing. By default Django stick to single database, however, if you want to implement more interesting database routing system, you can define and install your own database routers.
Database routers are installed using the DATABASE_ROUTERS setting. You have to specify this setting in your settings.py file
What you have to do is write one AuthRouter as described Django documentation Django Multiple Database
"Yes, but"
What you are looking for in the docs is called "database router".
There is even an example for the auth app in the docs there.
But, there is s serious drawback to consider with this approach:
We cannot have cross-database relationships in the models. If auth tables are in a separate database, this means that any otehr app that needs a foreign key to User model is going to run into problems. You might be able to "fake" the relationships using a db that doesn't enforce relationship checks (SQLite or MyISAM/MySQL).
Out of the box, such apps are: session, authtoken, and admin (and probably more).
Alternatively, a single-sign-on solution might do a better job: django-sso, or django-mama-cas + django-cas-ng, or the commercial Stormpath.
I am using a Django installation with MongoEngine to get MongoDB to work as my project's backend.
I've got the implementation to work but am stuck with Django Rest Framework's authentication system. Was just reading the API guide for their authentication chapter and was trying out their TokenAuthentication.
Tokens are created using
t = Token.objects.create(user=..)
and it expects a Django User instance. Since I am using MongoEngine, my database entry in the settings.py file is set to Dummy.
So how do I create a user instance that can be used by Token class.
I tried creating users using MongoEngine's mongoengine.django.auth but the Token class isn't accepting this object.
The resulting error is:
ValueError: Cannot assign "<User: gaurav>": "Token.user" must be a "User" instance.
Please let me know how I can get this to work.
Unfortunately you have to write it for yourself. As a reference you can use a gist I just created: https://gist.github.com/RockingRolli/79ceab04adb72c106cd6
I solved the issue a few weeks ago and it works. The code basically is inheriting the TokenAuthentication and adds Mongoengine specific behaviour.
There are also Django user features provided by Mongoengine: http://docs.mongoengine.org/en/latest/django.html#custom-user-model - IIRC you also need them for MongoTokenAuthentication.
All in all using Django (+Rest Framework) with Mongoengine can be tricky at some point and currently it looks like these issues will not be resolved soon.
I'm in the process of setting up a custom blog app using Django, with Mezzanine as the blog, which will be communicating with the auth system on my other Django app to handle all user authentication.
So far, I have set up an AuthRouter exactly like the example described here: Multi DB Setup in Django
However, my other Django app has a custom AUTH_USER_MODEL defined in its settings file.
As a result, when attempting to login to the blog, it is looking for a table with a different name from auth_user. I'm wondering if there is a way to specify the auth user table?
Thanks for any advice!
You need to have the code for your custom User model (or similar) in the blog project also. The you can set the proper table name with db_table (if auto generated is different). Then you will set AUTH_USER_MODEL in your second project to point to your custom model defined in the same project. You also need to modify your router to work with this custom user model.