Multiple user models in Django inherited from AbstractBaseUser - django

I have a "legacy" database with different tables for different user types (admins and customers). They have nothing in common. I need to implement both models in the Django project, users need to log in to the client pages, admins to the admin pages (not django admin).
How to solve this deal?
Basically, I have an idea to run two application instances with different config values AUTH_USER_MODEL but it looks not the best idea.
Thanks in advance.

Why not move the user account info like username password and name to django user and add extra role info to the role table linked by foreign key.

Related

How do we change Field Names in our existing Users table in django

I am trying to Edit Users Table in Django. I am using Users Table to login or register a users. I have to add a new field name Role in that Table but i can't find any option to edit that existing table in admin section.
i just try to field some files to field out where the code of that existing Table is but did't get it.
is there any way to Edit the Table or I have to Create a New Table and have to create a new method of registration.
i am not expert so it's hard to me understand things.
Well first, the Django admin interface it's just for performing CRUD operations over already existing models, you are not able to change in any way the database tables (at lest not using the "out of the box features") using the admin interface.
Said that in order to do what you want to do, with any model (not just User), you should:
Add the field to the model.
Instruct the admin interface to list this fields along the others.
Now the user model is kind of a special model here so I'll recommend a couple of readings you should complete before go forward with the model User customization.
References (User customization): Substituting a custom User model, Extending the User model.
And for the admin interface ...
Reference (admin interface): ModelAdmin options, special attention here to list_display

How to create a permissions model for accessing apps in a project?

I am creating a project with multiple services, each one represented as an app. I want to create a dashboard page where a user can see what apps they have access to, with staff users being able to add and remove apps via admin pages. What is the best model structure to do this? I.e. How should my models.py look? Is there a way to link such a table to the settings.py registered_apps tuple?
Sounds like what django admin do.
You can use django's permissions for that. Basically you assign permissions to groups and then you put your users in those groups (a user can be in several groups).

Django admin user

I am reading 2 quite popular django books, both recommend using django's admin user model, template to create user for the site I am building. It feels odd. Isn't it dangerous to add our site's users with the admin users that uses Django admin interface?
If that recommendation is right, how can I add more attributes to that admin user model (hence add more columns to the auth_user table)?
What they recommend is to use the Users model provided by the django.contrib.auth application. It's the standard in django and many other apps depend on this model to integrate with user data. Being the django admin one of them.
Note that not all users created using this model have access to the admin site. Only the ones with is_staff set to True.
To assign extra user information on the Users model you should use Profiles.

Adding fields to user's personal info in Django admin page

I just started a Django project (there are no apps in it). I activated the admin in settings file and can access the Django administration page. There is a column in Django page to add users; while adding users I get only three fields under personnal info, but I need to store some more information about users. I Googled around and found that I can use user profiles to accomplish this. I tried, but I am having problems.
My aim is to add three more fields to the user table:
role
contact number
other
I need details like: which function I need to write and where to do this.
I found this, but I do not know where I need to write these steps. I would greatly appreciate a more clear explanation of this.
Django User Profiles is what you need. The blog you linked to has clear steps on how to do it. You can check out the Django documentation. http://www.turnkeylinux.org/blog/django-profile also provides a good explanation.
Basically you need to create a new model with User as ForeignKey and define the model in the settings.py as AUTH_PROFILE_MODULE = "django_app.your_profile_modelname". Create the profile and save it just like any other model, and access it using user.get_profile()
Adding a couple of things in response to your questions below:
First, do not create apps as a directory. Use startapp <appname> [destination] as described here. That will create the app directory.
Second, you have to add the app to INSTALLED_APPS in the project's settings file, do a syncdb. Basically, follow the steps in Django tutorial on writing your first app.
Third, UserProfile is a separate model. It is not an extension of User. It is associated with the User just because you added User as the ForeignKey.
Fourth, to be able to see the user profile model in admin, you do exactly what you would do to add any other model to admin page. Create a file names admin.py under your app with:
from django.contrib import admin
from myproject.app.models import UserProfile
admin.site.register(UserProfile)
There are three key concepts to understand:
There is no built in "profile" system in Django, beyond the limited auth app which is really geared just to user login. You are expected to roll your own.
There is nothing magical about a profile record in itslef, it is just like any other record that takes User as a foreign key (or, more properly, a one-to-one field as per the docs). You create it by creating a custom django app (traditionally called profiles) and a model for that app (traditionally called UserProfile, since Profile is not allowed as a model name).
The only thing that sets UserProfile aparts as a model is that you specify it as the AUTH_PROFILE_MODULE which means that it is accessible when called .get_profile() on a User record. That's it. If you set up the UserProfile like so:
def UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
other fields
then you can also access the profile as user.profile rather than user.get_profile() which some people prefer.
Again, nothing magical about the profile model -- it is just a model record like any other model record.
If you want to be able to edit additional fields within the user form that's more complicated; easiest way is probable unregister User and then register it again using your custom ModelAdmin and form class but judging by your question you're probably not at that level yet.

Django LDAP: How to map extra fields in Active Directory to Django User Database

I would like to map some of the field available in my companies Active Directory to my Django Application like department, Job Title so on. I am using the Django_auth_ldap for backend authentication and it copies the data and updates the record in the Django application Database. Since, Django Auth User table does not support these attributes what will be your suggestions of doing it? Please let me know if you need any more information.
Read here, map AD profile attributes to the django profile.
http://packages.python.org/django-auth-ldap/#user-objects
Here is documentation from django https://docs.djangoproject.com/en/dev/topics/auth/customizing/. Have you tried googling? There is already lot of info available for this.