There is a CustomUser model inherited from AbstractUser. When registering a model in the admin panel, the password is displayed as text, that is, the type of this field is text, not password. I know that this problem can be solved if, together with the model, the ModelAdmin class is registered, in my case it will be CustomUserAdmin inherited from UserAdmin from django.contrib.auth.admin, and the password will be displayed correctly, but then the fields from CustomUser will not be displayed (as if the CustomUser model is registered, but only the fields of the User model will be displayed, when using a class inherited from the above-mentioned UserAdmin). What to do with this, please tell me!
you need to import django's function "make_password" if you need make a password:
from django.contrib.auth.hashers import make_password
and when you save the password in the db, do the following:
password=make_password(form.cleaned_data['password']),
django has already declared field for password user, you dont need to declared it again in the model that inherits AbstractUser.
Related
I have managed to add an additional field to the Registration form, "where did you hear about us?".
But I am not sure which files to edit in order to store the data from this field along with the users info.
i.e. When logging into the Admin section and go to "users" and view a users info I would like to see this field there.
Simplest way would be to store additional data in a UserProfile model about the user, e.g.
from django.contrib.auth.models import User
class UserProfile(models.Model):
# This field is required.
user = models.OneToOneField(User)
# Other fields here
where_heard_about_us = models.TextField()
You can then register the object as an inline object in your Django Admin
I'm using the Django User model for my users. What I need is to add an action on the User admin that will enable me to send a text message to the users I'll have selected.
This is how it's supposed to work:
I log into Django admin
Select the Users model
Select a user from the list of users and then select the "Send Message" action I have created.
I should be redirected to another page that will have the selected user names, and fields from a "Text Message" model that I have created to handle text messages.
So far I've had to create another model:
class UserProfile(models.Model):
user = models.OneToOneField(User) #extended the User model
phone_number = models.CharField(u'Number', max_length=20)
and then the form for the Text Messages:
class TextMessageForm(ModelForm):
class Meta:
model = TextMessage
fields = ('sender', 'to', 'message')
but I'm having trouble adding an action to the User model(raises an "model already registered error" which I solved by unregistering but then the passwords in the User model are viscible. That's why I created the UserProfile model)
So, can I be able to add an action to the User model without having to mess with how it'll display stuff on the admin?
Can I have the action redirect to the text form(or another template for that matter)?
Or is this a bad way to think of it... I'm using this API to send my texts. Any direction you can offer?
You could extend the UserAdmin admin page:
# admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
# Define a new User admin
class UserAdmin(UserAdmin):
"""My User Admin"""
# eg.
form = MyFancyForm
# or use my fancy template for the form
add_form_template = 'myadmin/user/add_form.html'
# or maybe add it to the change list
change_list_template = 'myadmin/user/change_list.html'
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
https://docs.djangoproject.com/en/1.7/topics/auth/customizing/#extending-the-existing-user-model
If you just want to add a button to go to another page to send a message, I'd modify the list template to add that button.
I'm trying to add a custom field to already existed django's User model.
I want all fields of default User model(including password hashing functionality) + a custom field has_car, so I did ...
class MyUser(AbstractBaseUser):
has_car = models.BooleanField(default=False)
and register in admin panel admin.site.register(MyUser)
when I try to add open this model in admin panel I get this error.
OperationalError at /admin/myapp/myuser/
(1054, "Unknown column 'myapp_myuser.id' in 'field list'")
I'm not sure if its a mysqldb error or what?
I know I can use OneToOne or ForeignKey field but I simply want to extend User model.
again, It django==1.7b4 + Mysql
If you're just looking to add custom fields to the standard User model, your user model should inherit from AbstractUser instead of from AbstractBaseUser.
Don't forget to set:
AUTH_USER_MODEL = 'myapp.MyUser'
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-django-s-default-user
I have a model called UserProfile which stores additional data about a django user.
This model has a OneToOneField to django.contrib.auth.models.User
I also have a post save signal on a User object that is fired on initial insert of a User which creates a UserProfile object linked to that User:
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
User objects can be created via the website front end which when saved creates the UserProfile. This all works fine.
I also have an admin.py file that allows a User and UserProfile object to be created via the admin console:
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from myproject.apps.users.models import UserProfile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = UserProfile
filter_horizontal = ['companies']
exclude = [
'field_1', field_n'
]
class UserProfileAdmin(UserAdmin):
inlines = [UserProfileInline]
admin.site.register(User, UserProfileAdmin)
When creating a new User via the admin console, the UserProfile form is displayed below the User form. If I leave all UserProfile fields as default (i.e. do not enter any data into the form fields or change any default values) the User is created successfully with the UserProfile. If however I attempt to add any values or update any defaults in the UserProfile form it falls over with the error:
IntegrityError at /admin/auth/user/add/
duplicate key value violates unique constraint "users_userprofile_user_id_key"
DETAIL: Key (user_id)=(6323) already exists.
This indicates the post_save signal on the User object has been called twice, both times the created flag that has been passed into the signal has been set to True resulting in an attempt in inserting a UserProfile object twice with the same User id. Django ends up rolling back the transaction and neither the User or UserProfile objects are inserted.
To add a bizarre twist if I completely remove the post_save signal everything works fine in the admin console and django appears to magically create the UserProfile object for me without even (apparently) knowing about it. Obviously this then breaks the front end as it relies on this post_save signal to create the UserProfile object.
I'm running Django 1.6 using PostgreSQL.
Any shedding of light on this situation would be hugely appreciated. Otherwise I think another approach to how the UserProfile is managed in the admin console will have to be considered.
I am trying to write custom get_profile() function which should create user profile for users who are registered thru admin or any other way where post_save was not called.
How can I start this?
I guess that you have a model to handle user profile like this:
class UserProfile(models.Model):
"""Contains user profile fields not provided by User model"""
user = models.OneToOneField(User)
# Defined User profile fields like picture, phone, etc
So adding following line (maybe in your models.py after UserProfile model):
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
Allows access to the profile of a user (i.e. in templates: {% user.profile.phone %}) by creating it if not exists.
This is the way I solved in my site a problem like you describe.
Hope this helps
I am a bit confused. Are you trying to let users create account and sign in? Then use django-registration which is easy and works out of the box.