I have a custom user model that is based on AbstractBaseUser, PermissionsMixin and I would love to use a package such as django-allauth and/or social-auth-app-django. I have done some research but did not find any tutorials or examples. Can somebody recommend anything?
You need to set the User model for the social auth package in your settings.py. For example:
SOCIAL_AUTH_USER_MODEL = 'app.YourCustomUser'
Read more about it here: https://python-social-auth.readthedocs.io/en/latest/configuration/settings.html#user-model
Related
I am having trouble finding where this admin file exists so I can add an extra field. I think it's auto-magically created upon setup.
I want to add a date field, specifically, to the listing page (shown below), perhaps after the UID field so I can know when the user auth was created.
screenshot of django user social auths listing page
Okay here's what I've tried using Django-allauth and I think it somehow works the same with django-socialauth. Just get the gist of the idea and work it to your code
Extend first the SocialAccountAdmin in any of your admin.py files, better if in a specific app like "user", "home", or whatever you prefer.
admin.py
from allauth.socialaccount.admin import SocialAccountAdmin
from allauth.socialaccount.models import SocialAccount
class MySocialAccount(SocialAccountAdmin):
list_display = ('user', 'uid', 'provider', 'date_joined') # I haven't tried just adding a certain list to the list_display, for the meantime add all necessary fields just like how socialauth did
admin.site.unregister(SocialAccount) # Need to unregister the default socialaccount admin
admin.site.register(SocialAccount, MySocialAccount) # Then register it back with the custom made admin
There may be perhaps a better way to do this but this did the work.
Can it be interesting to just add a field to your model ? Adding a DateField for your creation date. Probably you need to understand learn more with : https://docs.djangoproject.com/en/3.0/ref/models/fields/
Hello everyone let me ask something about the admin interface and how can I handle my users by app.
Well so sorry I'm nooby first of all. So, I need to create a new app for basically take some quiz so I will need users and these users within my app should have a different role as a student or teacher and so on.
The thing is that I don't know if the admin Django interface is just for the DB models or whether I can use it as a security layer in my app.
Or otherwise, I should create a buck of tables in my app model for users and roles and then handle everything from the model because the admin is just the DB access. what could you tell me? thanks so much.
From Django's documentation about Django admin - Link
model-centric interface where trusted users can manage content on your site.
Django comes with a user model, you can extend it to represent teachers and students as described in django's documentation here, you would create ModelAdmins and register your models. Beyond that you can manage your users easily through the admin system, example code:
models.py
from django.contrib.auth.models import User
from django.db import models
class Teacher(models.Model):
user = models.ForeignKey(User, related_name='teacher')
class Student(models.Model):
user = models.ForeignKey(User, related_name='student')
admin.py
from django.contrib import admin
from .models import Teacher, Student
admin.site.register(Teacher)
admin.site.register(Student)
As for security, it is not clear what you mean by "use it as a security layer in my app", if you elaborate more, people can better help you. You can generally learn about security in django here.
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
Hello I am very new to Django, and I am making an app that stores a lot of information about the user. But django's auth app stores only a bit of information about the user. Is there any way I can create my own model "User" and make Django's auth app use this model. Thanks.
This has been supported since Django 1.5, and is fully covered in the documentation.
Basically, you need to subclass auth.models.AbstractUser, and set the AUTH_USER_MODEL setting to point to your new model.
You can solves your problem with UserProfile model. And you can store the user extra information in this with relation of onetone or ForeignKey with unique property field.
Django user profile
U can multi-table inheritance the user model
from django.contrib.auth import User
class MyUser(User):
//add ur required fields
Reference for in heritance.
I work on django project that migrate from django social auth to python social auth.
Previously new social auth user first name/last name will be saved automatically for first time login.
Now after using python social auth, it's not.
Seems I have to use this setting:
SOCIAL_AUTH_USER_MODEL
but
SOCIAL_AUTH_USER_MODEL = 'django.contrib.auth.models.User'
generate error when invoking runserver:
django.core.management.base.CommandError: One or more models did not validate:
default.usersocialauth: 'user' has a relation with model web.models.User, which has either not been installed or is abstract.
Wanted to try subclassing User model in the project
from django.contrib.auth.models import User
class User(User):
but that is not feasible right now.
Also saving manually the name from response data in custom pipeline is prohibited as well.
Really want to know if there any other solution?
Thanks.
remove SOCIAL_AUTH_USER_MODEL because you are using Django Default User model.