logeeks
logeeks

Reputation: 4977

where to write code to extend django user table and capture the user authentication details

I need to extend the user model provided by the django framework to accomodate extra fields. I learned from the django tutorial that the best option to do that is to have a separate table, which stores information related to users personal details.

1 )Where is the best place or which is the best file to write the code to extend the user table?

2) I also want to check the status of the users, such as whether he is logged in or not or what group he belongs, whenever a request hits the server. Usuallly, while i was using Kohana framework I write a base class, which is then extended by each view, I write all the code require in the before() method, which the kohana guarantees to call first, on each request before passing control to the actual method. How can I implement similar concept in Django?

Thanks for your attention.

Upvotes: 1

Views: 57

Answers (2)

sinitsynsv
sinitsynsv

Reputation: 893

1) Create an app, accounts for example, and put your users profiles models (or create custom User model) in models.py file

2) You must check the user permissions in the views.py. If you use function views you can create decorators with your checks as described here. Also take a look at existing decorators. If you use class based views you can create decorators or create mixins, described here. There is apps that allready have some mixins, for example django-braces

Upvotes: 1

wolendranh
wolendranh

Reputation: 4292

There are two way to do this, according to Django docs. Extend existing User:

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. For example you might create an Employee model:

And Substituting a custom User model¶

Some kinds of projects may have authentication requirements for which Django’s built-in User model is not always appropriate. For instance, on some sites it makes more sense to use an email address as your identification token instead of a username.

There are examples for both approaches in docs.

Upvotes: 1

Related Questions