Add foreign key in the auth user model - django

I'm new in Django and I'm trying to add a foreign key in the auth user model. I have different users, and each user has an unique university, but an university can have many users. I'm using django 1.6.
I found a lot of information about using User model as a foreign key, but almost nothing about adding a foreign key to the User model.

It is possible, please read the docs.
There are two ways to extend the default User model without
substituting your own model. If the changes you need are purely
behavioral, and don’t require any change to what is stored in the
database, you can create a proxy model based on User. This allows for
any of the features offered by proxy models including default
ordering, custom managers, or custom model methods.
If you wish to store information related to User, you can use a
one-to-one relationship to a model containing the fields for
additional information. This one-to-one model is often called a
profile model, as it might store non-auth related information about a
site user.

Related

Extend Django user model with custom fields, receivers and backend

I am designing a Django application (v1.6) and need to do several things with users:
Add custom fields, such as a foreign key for user department
Trigger database changes when certain fields change. For example, when the user's department changes I need to move inventory in another model out of the old department and into the new. I was planning to use a pre_save receiver to do this.
Define custom permissions, such as a user can only modify rows in a table that are associated with their department.
Eventually I want to integrate the application with our Active Directory server for authentication.
I looked at the options in the documentation and see that there are several options, from extending the user model with a one-to-one relationship to writing a custom user model.
What design should I use to meet all of the above goals?
Take a look at this blog post: it provides all the design principles to achieve your goals.
http://www.roguelynn.com/words/django-custom-user-models/
I would also take a look here for more information about Configurable User Models, if you want to have your own authentication scheme:
http://procrastinatingdev.com/django/using-configurable-user-models-in-django-1-5/
I also found the following reference helpful: http://www.sofokus.com/blogi/custom-user-model/

Custom User model for Django with Facebook Login

On the client side I use the iOS SDK for Facebook to login and I get the Facebook ID and the access token.
Now on the Django side of things I would like to create a user with Facebook ID as the primary identifier and other fields like access token, first name, last name etc (the last two of which I will retrieve from the Graph API on the server side).
I know that I have to create a custom user model.
If you wish to store information related to User, you can use a one-to-one relationship to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user.
This will not be enough as I will be using the Facebook ID and the access token for authentication.
This leaves me with two options: I can substitute a custom user model like so:
AUTH_USER_MODEL = 'myapp.MyUser'
Or I can subclass AbstractUser:
If you’re entirely happy with Django’s User model and you just want to
add some additional profile information, you can simply subclass
django.contrib.auth.models.AbstractUser and add your custom profile
fields.
But that doesn't sound quite right either. Also this design tip has confused me a little more.
Model design considerations
Think carefully before handling information not directly related to authentication in your custom User Model.It may be better to store app-specific user information in a model that has a relation with the User model.
What is the best way to implement what I am trying to do?
Just a side note: The problem of a custom user is that it is often the case that other apps (and yes, you will use them) don't interact correctly with it due to the assumptions they make on the base model for auth.
This will not be enough as I will be using the Facebook ID and the access token for authentication.
I'm not sure you really need a custom user. For instance, I'm using open id for authentication and there is no problem in using the default user: there is just another model with a OneToOne relationship to the default user.
The main concern you should have for a Facebook ID for authentication (and authentication in general) is to have a custom authentication Backend with its own specific facebook authentication.
Internally, authenticate() runs through all installed backends (settings.AUTHENTICATION_BACKENDS) and tries to authenticate the user with one of those.
You can search some of the existing implementations e.g. in Django packages for facebook authentication.
If your users should be enabled to login/register with username, mail and password -> use a OneToOne relationship to django's usermodel to store facebook credentials.
If your usermodel entirely depends on facebook data and you don't want your users to login with username/pass -> substitute the usermodel with AUTH_USER_MODEL = 'myapp.MyUser'.
You might also want to take a look at django-allauth which solves much of your problems in a sweet little package.

django - user_type of User in another model

I have a model like Community, where some users can be admins and other users are normal users. I can link user the community as foreignkey or someother possible relationship. But, how can I implement user_type ? Where should I have this field ? Is it just another field in the model Community ?
I guess you want to provide different access levels to user in the community based on their user_type.
In that case, you can either keep a FK from UserProfile to Commmunity(in case one user will only be part of one community).
If users can be part of various communites, you should keep a ManyToManyField called users on Community model.
After this just keep these user in groups (See Django group and Permission). Set permission on Groups you define. Based on what group a user in, he will have the group permission. Use these permission to decide what access to give to a particular user.
Depends on the size and other attributes of the data but a common way to go about it would be to have a one-to-many relationship between Community and User. Then you can have a field in User that states the user type.
In the database you will have a Community table and a User table which are linked via foreign key, and the User table will store the user type information.

Should I ForeignKey to Django User or a Profile model?

I'm wondering what people's thoughts are on joining models directly to the auth.User object vs to the user's profile model.
I'm storing some different types of models which my user are adding in my app. App users will search for other users via criteria on these models.
On the one hand, I'm thinking that if I join straight to User then I won't need to do request.user.get_profile() each time I need to grab the User's records, and it doesn't presuppose that a User always has a profile (they do in my app at the mo, but still). This leaves the profile model as just containing the user's contact details.
On the other hand, I imagine I'll most likely need values from the Profile (eg name, location) when I'm looking up these other models.
No doubt either will work, so maybe it doesn't matter, but I just wondered what other people's thoughts were.
Thanks!
Ludo.
I would also recommend creating foreign-keys to the User model. It just makes your life simpler when working with the user object in the view, for one. So, you can do things like request.user.foo_set, etc. without having to go through the profile model.
In general: If you want to make your apps reusable, always create foreign keys to User model.
As you already said, in most cases you will need User as well as Profile instance, so to prevent multiple database queries, use cache.
If reusability isn't relevant, create foreign key to Profile and use select_related() to get User instance with single query.

Is it okay to use django's auth_user model for storing user's info

If I am trying to create a login app for my project is it advisable to use auth_user as the model to store newly created user's in and just extend in in my signup app through:
from auth.models import User
class UserForm(ModelForm):
class Meta:
model=User
Or is it better to create a custom model to store users in. Will what I have above even work? I'm sure the answer is either "of course, that's what it's there for" or "dear god why would you want to do that" but I don't know which.
User is meant to store the credentials of users that will be able to log into and use the system via normal Django auth mechanisms. If your project will use Django's auth then yes, User is the correct model to use.
The short answer is yes. and if you want to store additional information of the user, you could create another model and a foreign key to the user model.