Hey I'm new to allauth package.
I would like to know how to write a user model that extends the all auth user.
for example i would like to have a user that has allauth user fields in it and also an image, list of favorite text field.
I would realy appreciate any help!!
You can extend the Django user model using an OneToOneField(User).
Here's the relevant part of the Django docs: Extending the existing User model
IF you need it you can use a custom user model but this will be more difficult. In this case see allauth custom user models and Django documentation
Since you want allauth user fields with some extra fields, you can inherit default user model and add your custom fields to it.
from django.contrib.auth.models import AbstractUser
class ExtendedUser(AbstractUser):
favourites = models.charField(max_length=255)
....
....
In your settings.py file, add this line
AUTH_USER_MODEL = 'users.ExtendedUser'
To store the extra fields when user signs up, use django-allauth signals as in your views as follows.
from django.dispatch import receiver
from allauth.account.signals import user_signed_up
#receiver(user_signed_up)
def user_signed_up(request, user, **kwargs)
#get extra fields from kwargs and save to database
Related
I have a Profile model that has a one to one relationship with the User model from django.contrib.auth.models , when I add a new profile from the admin panel I noticed that the password field is not transformed into dots when typing , it shows the actual password , that's not the issue though , the issue is , when ever I create a new profile which in turns creates a new User , and mark the is staff attribute as true, I can't log in into the admin panel with that created account, unless I reset the password manually with
python manage.py changepassword the-user-name
after I have reset the password and only then even with the same password, I can log in into the account normally, does anybody know why is that happening ?
I made sure it's not a mistake in typing the password and I tried it several times until I made sure that this is what is actually happening
Update
when I enter the Users model from the Authentication and Authorization section opposed to entering it from the app name, I find that written besides the password section
Invalid password format or unknown hashing algorithm
how can I fix it ?
I forgot to say that I am using an UpdateView from generic views , and using the model Profile as the the model attribute set on that view
the code of my update view
class ProfileUpdate(UpdateView):
model = Profile
fields= [
'username',
'bio',
'avatar_thumbnail',
'location',
'tags',
'contact_information'
]
def get_object(self):
return Profile.objects.get(pk = self.kwargs.get('user_pk'))
def get_queryset(self):
base_qs = super(ProfileUpdate, self).get_queryset()
return base_qs.filter(username=self.request.user.username)
That would inherit from ModelAdmin instead of UserAdmin in your admin.py.
Can you instead try this:
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from app.models import Profile
class ProfileAdmin(UserAdmin):
def __init__(self, *args, **kwargs):
super(UserAdmin,self).__init__(*args, **kwargs)
UserAdmin.fieldsets += (('profile', {'fields': ('attr1', 'attr2')}),)
admin.site.unregister(User)
admin.site.register(Profile, ProfileAdmin)
Explanation
When you registered the Profile admin like so, admin.site.register(models.Profile), internally it was inheriting from ModelAdmin class which is perfect for all other classes. But for User we need hashing for saving the passwrd field, so there is a separate class UserAdmin that must be inherited from.
Once you inherited, the password issue was resolved but since the base model was UserAdmin, only user related fields were showing. We needed to append the profile fields for which we added the __init__ method and appended the profile fields to the admin section.
Good evening, I am using django-registration for a Web application; By using this, the form that has default django-registration only has 4 fields (username, email, password, repeat password). How to add more fields to this form and store them in the database correctly? Thank you.
I haven't used django-registrations but I found a link which might be helpful to you.
All you need to do is extend the class 'RegistrationForm' and add more fields.
Something like this:
forms.py
import from registration.forms import RegistrationForm
class CustomRegistrationForm(RegistrationForm):
extra_field = forms.EmailField()
extra_field_2 ...
Then, handle the form POST and save these details.
http://django-registration.readthedocs.io/en/2.0.1/forms.html
If I create a CustomUser model which inherits from django.contrib.auth.models.User, like so:
in models.py
class CustomUser(django.contrib.auth.models.User):
customfield = TextField()
...
Should I still be able to use
django.contrib.auth.{authenticate, login, logout} in the normal way? Do I have to make some additional configuration change? I know these methods only work on User objects, but technically my CustomUser is-a User.
Currently, authenticate(username=u, password=p) is always returning None, even with valid credentials.
Since Django 1.5 (officially but it doesn't worked for me) and "stable" in 1.6 there is a functionality to extend the User model in a clean way.
At first:
-> Take care that you load the User model only via:
from django.contrib.auth import get_user_model
User = get_user_model()
-> Once you have built the database theres no easy way to change the User model. The database relations will break and Django / South isn't able to fix it.
-> third party modules have to be compatible with that new layout and refer in it's models to "get_user_model()", too.
You have to add some Code for the admin to respect your new model:
See: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model
To Override the model you need to inherit from AbstractBaseUser:
from django.contrib.auth.models import AbstractBaseUser
class MyUser(AbstractBaseUser):
...
date_of_birth = models.DateField()
height = models.FloatField()
...
REQUIRED_FIELDS = ['date_of_birth', 'height']
AbstractBaseUser provides you all attributes of the default user model. So you don't have to take care of email, username, first_name, last_name, password etc.
More info about overriding the user model: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.CustomUser
In your settings link your new model:
AUTH_USER_MODEL = 'customauth.MyUser'
Please read the whole documentation of customizing the user model, there are some interesting hints for overriding the default manager, admin forms etc. Just remember that bigger changes in an existing project can be a big pain.
A short overview:
- Extend models.AbstractUser
- Set AUTH_USER_MODEL in settings.py
All details can be found here: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model
Hi ive not be able to find any one trying to solve this issue yet so i thought id ask the question here. Basically im trying to add an image field to the admin.auth users admin site that relates to my userprofile model.
Aswell as users that register to the site, the admins need to be able to specify a profile picture when the create a user in the admin site that will be used for posts they make to news and blogs etc.
Is this at all possible, i have the userprofile model working which will allow me to add the image but i want to include this in the admin user panel when an admin creates a new user.
Any help is greatly appreciated!!
Thanks in advance
You can add your UserProfile (with the extra image field) as an inline to the User adminModel. So when you go to the admin.auth panel to add a new user, there will be an inline below it with all the UserProfile fields.
Something like this should do it (in admin.py of whatever app your userprofile is in):
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib import admin
class UserProfileInline(admin.StackedInline):
model = UserProfile
class UserModelAdmin(UserAdmin):
inlines = [UserProfileInline,]
admin.site.unregister(User)
admin.site.register(User,UserModelAdmin)
I'm trying to extend Django Admin Login. Most of the resources pointed towards extending views after the Login.
I wanted to add sites to the Login criteria.
So instead of
Username
Password
It will be
Username
Password
Site
Such that the Site will check whether the user belongs to the Site as admin and if it is, it will load only data belongs to the site.
Thanks
Cheers,
Mickey
I am not sure because I am newbee in django.
I would copy the admin code from the original django code in my profject folder. Then I would change it like you want it and put it in installed apps.
I hope, I could help you. As I said, I am a newbee in django.
Craphunter
You use user profiles for this.
Here's a basic example (this code would go in your app's models.py):
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
# Define a signal handler to be called after saving
# a User object
def user_save_handler(sender, **kwargs):
# If the save was creating a new user as opposed
# to updating an existing one, then create a
# UserProfile object and associate it with the User
if kwargs['created']:
# kwargs['instance'] is the User object we just
# created
UserProfile(user=kwargs['instance']).save()
# Hook the signal handler up to the User model's post_save
# signal
post_save.connect(user_save_handler, sender=User)
# Define your UserProfile class as usual.
class UserProfile(models.Model):
# user is a one-to-one reference to the associated
# User object. You need this field
user = models.OneToOneField(User)
# Now define any other fields you care about
birthday = models.DateField()