I just included python social auth and added Facebook login. I have a Profile model where I save some user data like "description" and "username".
class Profile(models.Model):
username = models.CharField(max_length=75)
user_des = models.CharField(max_length=250, blank=True, null=True)
...
How do I relate the user's Facebook account to the existing model, and where do I save that relation?
Looking at the social auth docs I found that you can specify custom user models using this in your settings.py:
SOCIAL_AUTH_USER_MODEL = 'myapp.CustomUser'
In your case it will be:
SOCIAL_AUTH_USER_MODEL = 'myapp.Profile'
The docs I found this on are here:
http://django-social-auth.readthedocs.org/en/latest/configuration.html#custom-user-model
They also include documentation of ORMs here:
http://django-social-auth.readthedocs.org/en/latest/configuration.html#orms
edit:
By default social-auth uses the
django.contrib.auth.User
model to save users, the documentation can be found here:
https://docs.djangoproject.com/en/dev/ref/contrib/auth/
Related
I have model.py file which has one model named as Tutor
class Tutor(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
password = models.CharField(max_length=255)
I want to implement token authentication using this Tutor model, for login and signup. As of now I have setup token authentication and its working good, but its default authorisation is for USER, which get generated using python manage.py createsuperuser, and I want to use Tutor for Authroization.
How to go building this, any resources for my usecase would mean a lot :)
You can change your default auth model by updating the AUTH_USER_MODEL = "<app_name>.<model_name>" in settings.py
In your case, if your app name is User, it would be AUTH_USER_MODEL = "User.Tutor"
I am creating my first Django (ver 3.1) website which is simply a blog with a home page and store page. I want to create custom user groups that define specific roles (with unique permissions) for my blog.
These groups are:
Reader - anonymous viewers and new accounts; has read permission for all content only
Author - login required; has read and create permissions; edit and delete permissions for own content only
Moderator - login required; has all CRUD permissions for all content
Admin - login required, has all permissions (superuser)
All new users by default are in the Reader group. Author would either be assigned manually or eventually by an online form application to determine eligibility. Moderator and Admin would of course be manually assigned.
I am approaching this with possible future development, such as allowing user groups to be easily extended to other website pages. For example, a 5% discount for Author users applied to all store items, etc.
Which approach to creating user groups would be best for my situation? I have seen it done within the Django Admin Panel and by creating custom User Models via extending the AbstractBaseUser and UserBaseManager classes.
I think extending the AbstractUser model is a good approach. How about something like this?
class CustomUser(AbstractUser):
"Define the extra fields related to User here"""
first_name = models.CharField(_('First Name of User'), blank=True, max_length=20)
last_name = models.CharField(_('Last Name of User'), blank=True, max_length=20)
# - - - Some more User fields according to your need s
class Meta:
permissions = (("can_read_content", "To provide read facility on all content"),
#---------------------
)
I using django from last 4-5 months and recently started learning django-rest-framework and I'm confused about proper authentication system,
Actually I am trying to build an application mostly using REST API because my
client can be both browser and Android,
so I need an authentication system in which user can sign up using both django
built-in auth(django.contrib.auth.model.User) as well as third-party social
authentication(Google, Facebook, etc..).
Now, I'm confused about how do I create my database, because when ever i'll create
a table/model lets say a 'Book', then this model would need a foreign key to the user model and here user can be both 'django.contrib.auth.model.User' and a user signed-up using third party auth,
So how I will refer to User in foreign key Field of my models?
And I have also decided to customize django's buit-in auth because i want
user to login using their email not username.
class Book(models.Model):
title = models.CharField(...)
author = models.ForeignKey(?) ? Here, how do i refer to both
'django.contrib...User' and users signed-up
using thrid-party auth.
Let me elaborate on your question.
First of all: You're lucky. There's an (almost) out of the box version for your problem.
For social and normal authentication and registration, including email verification etc. you can rely on django-allauth:
https://github.com/pennersr/django-allauth
django-restauth provides a restful platform built on top of all-auth, so that you don't even have to start building your auth rest api from scratch:
https://github.com/Tivix/django-rest-auth
When it comes to your db schema, there are a few options. You could go ahead and build your own authentication system, which, in my opinion, is overkill.
Rather more, I would implement a profile model, which has a OneToOne relationship to the User model from django.contrib.auth.models.User as described in this chapter of the Django docs.
Your models (of course in separated apps) would look like this:
from django.contrib.auth.models import User
from django.db import models
#other imports
class UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
books_read = models.IntegerField(default=0)
books_recommended = models.IntegerField(default=0)
class Book(models.Model):
title = models.CharField(...)
author = models.ForeignKey('UserProfile', related_name='books')
Another question you will run into is how to update and/or display those nested relations in your serializers.
This FAQ article from the django-restauth docs and this chapter of the official django-rest_framework docs will get you jumpstarted.
Best,
D
The Django-Project contains a REST-API, based on TokenAuthentication and a Web Login to the Backend with the Django Auth User.
The Problem is: I found no way to specify two different AUTH_USER_MODEL'S for each authentication individually.
For the REST-API users, I defined a Custom User Model:
class User(models.Model):
id = models.IntegerField(primary_key=True)
name = ....
....
USERNAME_FIELD = 'id'
REQUIRED_FIELDS = []
In the settings I added AUTH_USER_MODEL = 'backend.User', so that the rest_framework Token authentication references to the right user model.
But when enabling the django.contrib.auth.views.login for the Web-Backend, this also uses the backend.Usermodel for authentication. But it should use the django built in user model.
How can I specify a second user model specifically for either the rest framework api or for the django.contrib.auth.views?
I have a Device model in django and I would like to be able to authenticate it.
class Device(models.Model):
device_key = models.CharField(max_length=100)
udid = models.CharField(max_length=100, unique=True)
To be more specific, given the above Device model, I would like to be able to achieve something similar to TokenAuthentication (http://django-rest-framework.org/api-guide/authentication.html#tokenauthentication).
Sending a request to a login URL with the device_key and udid should return a token, which identifies the Device model, and can be used for further requests.
Note:
Devices are NOT Users. I already use the User model for different purposes.
This question is basically similar to asking how do I authenticate a custom user model in django, which does not have a decent answer either (Django custom User model authentication)
Is there a "django" way to do this?