add an image field to admin.auth admin site in django - django

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)

Related

Django: 'no such table' after extending the User model using OneToOneField

(Django 1.10.) I'm trying to follow this advice on extending the user model using OneToOneField. In my app 'polls' (yes, I'm extending the app made in the 'official' tutorial) I want to store two additional pieces of information about each user, namely, a string of characters and a number.
In my models.py I now have the following:
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
stopien = models.CharField(max_length=100)
pensum = models.IntegerField()
and in admin.py the following:
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from polls.models import Employee
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline, )
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
When adding a user using the admin panel my two new fields display correctly. However, when I click 'save', or if I don't add any user and just click on the name of my sole admin user in the admin panel, I get the following error:
OperationalError at /admin/auth/user/1/change/
no such table: polls_employee
I see some questions and answers related to similar problems, but they seem to be relevant for older version of Django. Could anyone give me a tip as to what I should do? Ideally I'd want my two additional fields display in the admin panel, though I suspect this might be a task for the future.
I have to confess I do not understand this paragraph from the documentation just following the advice I'm using:
These profile models are not special in any way - they are just Django models that happen to have a one-to-one link with a User model. As such, they do not get auto created when a user is created, but a django.db.models.signals.post_save could be used to create or update related models as appropriate.
Do I need to tie this 'post-save' to some element of the admin panel?
I'd be very greatful for any help!
You need run makemigrations to create a migration for your new model, and then migrate to run the migration and create the database table.
./manage.py makemigrations
./manage.py migrate

Django-Oscar un-registering a class from admin panel

I would like to un-register a ModelAdmin class from the default oscar app.
I created an admin.py file and did the following but was unable to obtain the desired result.
from django.contrib import admin
from oscar.apps.address.admin import *
admin.site.unregister(UserAddressAdmin)
admin.site.unregister(CountryAdmin)
The model is still displayed in the admin panel.
I want the model to be created but I do not want to display the same in the admin panel.
Any help would be greatly appreciated. Thanks.
admin.site.unregister takes the Model class that you want to unregister - you are passing it a ModelAdmin class, which it will just ignore. This should work:
from oscar.core.loading import get_model
admin.site.unregister(get_model('address', 'useraddress'))
admin.site.unregister(get_model('address', 'country'))
However this is all unnecessary! Why don't you just put an empty admin.py file in your fork of the app? Currently you are importing Oscar's admin registrations only to manually unregister all of them again. Just don't import them.

Django user with extra fields- extending User

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

How do staff users edit their profiles in Django admin?

I'm new to Django. I'm working on a website that will only contain users as staff users. I'm the administrator. My plan is to register a user and give him his username and password to login; I want him only to be able to edit his profile to add more information, not to change existing attributes. How can I do that?
The easiest way would probably be to create a appropriate ModelForm and a corresponding view, that checks that the instance the user want's to update is the own instance.
I you want to integrate this directly in the admin backend, you can also do this. The most djangoish way would propably be to create a own ModelAdmin class for the User that has the right methods overridden (see the methods startinghere). I think you should start with overriding has_change_permission where you can check if the object the user tries to edit, is his own and return False otherwise.
To replace the standard User ModelAdmin you need to do a little fiddeling in the admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyUserAdmin(UserAdmin):
# add the code here
# deregister the standard AdminModel and register the own one
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
(I'm using this code on a live site and it works create)

Django admin - stackedInline single instance

I'm building a site based on a highly customized django admin instance and am running into issues with user profiles as an inline to user_admin
long story short regardless of what I set for max_num and extra in the admin.StackedInline instance it allows up to 2 profiles per user - with a blank one in place by default if the user has an existing profile
anyone know how I could adjust this to show only a single inline profile without resorting to some JS front end hack?
relevant code from: profiles.admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from profile.models import user_profile
class user_profile_admin(admin.StackedInline):
model = user_profile
fk_name = 'user'
max_num = 1
extra = 0
class user_admin_extended(UserAdmin):
inlines = [user_profile_admin, ]
admin.site.unregister(User)
admin.site.register(User, user_admin_extended)
I assume you're using FK field to connect user and profile? Try OneToOneField it should render just one inline in admin.