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.
Related
previously we implemented one django application call it as "x" and it have own database and it have django default authentication system, now we need to create another related django application call it as "y", but y application did n't have database settings for y application authentication we should use x applications database and existing users in x application, so is it possible to implement like this?, if possible give the way how can we use same database for two separated django applications for authentication system.
Sorry for my english
Thanks for spending time for my query
So, to achieve this. In your second application, add User model in the models.py and remember to keep managed=False in the User model's Meta class.
Inside your settings.py have the same DATABASES configuration as of your first application.
By doing this, you can achieve the User model related functionality with ease in your new application.
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
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 want to know if I've understand the main point of django app usage.
Every app has a models.py file which create tables in our database, correst?
For example I want to create a personal CMS. I should create an app, to create tables for my posts details, and should create an other app to creating tables for my users that want to sign up into my blog, in order to keep their username and password in the database, and I also can create an other app to create a separate tables to save other data..... Do I think correctly?! What are django apps exactly for?
Apps are logical modules. One app can contain several models. Your project could have users and blog apps. users would have User and Group models, blog would have Post, Tag and PostTag models.
Views within single app usually have same URL prefix and their own URL routing.
Within app all database migrations are executed consecutively whereas it's your responsibility to specify dependencies between migrations from different modules.
Try to keep logical bounds between apps as weak as possible.
I want to make two apps in my Django project - one for web pages (browser) and one for api calls (mobile app). Both apps should use same users for authentication.
So how should I implement it? I want to make one more app for users, but is it the right way to make app with just one model and without any other logic?
You can use Custom User by extending the existing User model in Django. You can refer Extending user Model for more details.
Hope this helps.