Django: I dont know want is the wrong with my code - django

So I was trying to add an imagefield in my model using the UserModel, so I made this models
from django.contrib.auth.models import User
from django.db import models
class ProfileImage(models.Model):
user = models.OneToOneField(User,
on_delete=models.CASCADE,
editable=False)
avatar = models.ImageField()
def user_avatar(self):
return self.profileimage.avatar
User.add_to_class('user_avatar', user_avatar)
And I made an admin to see the imagefield in the users,
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
# Register your models here.
UserAdmin.fieldsets += ('Custom fields set', {'fields': ('user_avatar',)}),
I dont kno what is wrong with my code, when I open a user to see the image field and , does anyone know what is the problem?

In admin fields and fieldsets expects you to list actual columns in the database. user_avatar is a function you've written so you can't list it as a field on the User model because it doesn't exist in that table.
Make a more generic model for all types of user information, not just an image, like this;
class Profile(models.Model):
"""
Profile model
"""
user = models.OneToOneField(
verbose_name=_('User'),
to=settings.AUTH_USER_MODEL,
related_name='profile',
on_delete=models.CASCADE
)
avatar = models.ImageField()
Then you can access the avatar with request.user.profile.avatar or in your admin register your Profile model as an inline to the User;
from django.contrib import admin
from django.contrib.auth import get_user_model
from myapp.accounts.models import Profile
User = get_user_model()
class ProfileInline(admin.StackedInline):
model = Profile
max_num = 1
can_delete = False
class MyUserAdmin(admin.UserAdmin):
inlines = [ProfileInline]
# unregister old user admin
admin.site.unregister(User)
# register new user admin that includes a UserProfile
admin.site.register(User, MyUserAdmin)

Related

User Model Customisation in Django

I want to edit the User Model in admin.py but I am not able to figure out how can I do this?
Here is the Image of Admin Panel of User Model
Can someone please help me? I want to add some customized fields in the User model.
You can do that by extending AbstractUser from django.
# models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.translation import gettext_lazy as _
class User(AbstractUser):
EMAIL_FIELD = 'email'
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
email = models.EmailField(
_('email address'),
unique=True,
blank=True
)
cellphone = models.CharField(
_('cell phone'),
max_length=20,
null=True, blank=True
)
Then you also need to specify this Custom user model in your settings. Specify path to the user model you've just created. <app_name>.<ModelClassName>
# settings.py
AUTH_USER_MODEL = 'users.User'
Lastly, your admin must also inherit from Django default UserAdmin, if you want to save your time from all the hassle of creating some methods they have already created. Now you can edit user admin the way you want by also getting advantage of all the
existing admin features.
# admin.py
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin as OrigUserAdmin
User = get_user_model()
#admin.register(User)
class UserAdmin(OrigUserAdmin):
list_display = (
'id', 'first_name', 'last_name', 'username', 'email', 'is_active'
)
use 'AbstractUser' model to Extend pre define user model in Django.
then we cam easily add some field or add more information in usermodel.
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
Then we have to update our settings.py defining the AUTH_USER_MODEL property.
AUTH_USER_MODEL = 'core.User'
follow this link for more information:-https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html

How to make custom search functions

How can i make a custom search function in django admin. I want to search all fields of the database tables and to find and retrieve the matches.
Is it possible?
Lets take an example of custom User models with below fields,
app_name/models.py
from django.contrib.auth.models import AbstractBaseUser
class User(AbstractBaseUser):
first_name = models.CharField(max_length=50)
middle_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(max_length=100)
username = models.CharField(max_length=100)
Now the admin for this models would be,
app_name/admin.py
from django.contrib import admin
from app_name.models import User
class UserAdmin(admin.ModelAdmin):
search_fields = User._meta.get_all_field_names()
admin.site.register(User, UserAdmin)
User._meta.get_all_field_names() will return you a list of all the fields of User model.

Django user profile creation happening twice

I am seeing an error with Django user profile creation that has been asked before a few times, but the solutions to the other questions and the Django docs recommended fix is not working.
I have a UserProfile model that I am trying to create when creating a new user object from the Django admin pages.(forgive me on syntax here, it has been a while since I used stack overflow)
class UserProfile(models.Model):
user = models.OneToOneField(User)
organization = models.IntegerField(null=True)
phone = models.CharField(max_length=20, null=True, blank=True)
phone_ext = models.CharField(max_length=8, null=True, blank=True)
team_no = models.CharField(max_length=30, null=True, blank=True)
post_save.connect(create_user_profile, sender=User, dispatch_uid="a.unique.string.that.does.not.help.me")
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])
I have added the user profile to the UserAdmin class and unregistered/registered the admin site entries.
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'profile'
class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )
actions = [set_organization]
exclude = ('is_staff',)
try:
admin.site.unregister(User)
admin.site.unregister(RegistrationProfile)
admin.site.unregister(Group)
## Re-register UserAdminadmin.site.unregister(User)
finally:
admin.site.register(User, UserAdmin)
admin.site.register(RegistrationProfile, RegistrationAdmin)
admin.site.register(Group, GroupAdmin)
So each time that I try to go create a new user I always get a duplicate key error for the user_profile_user_id field. Looking at the logs there is two insert statements being ran on the user save. The first is missing all data input into the user profile fields in the admin interface. The second has all the data but fails since there was already the initial insert with just he user_id.
I have searched the project over a few times looking for a duplicate import of my models.py for the UserProfile class and have found nothing.
Does anyone have any ideas here?
Thank you.
--update --
Django version is 1.3
imports in the profile/models.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save, pre_save
import logging
imports from the admin.py
from django.contrib import admin
from django.contrib.auth.models import Group
from test.django.registration.models import RegistrationProfile
from test.django.registration.admin import RegistrationAdmin
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from django.contrib.auth.models import User
from test.django.extranet.profile.models import UserProfile
from test.django.common.models.ajrsonline.ajrsonline_users import AjrsonlineUsers
from test.django.common.middleware.ajrs_ldap import AJRSOnlineLdapBackend
Use if statement so that the first one will be restricted. Like this for example:
if instance.field_name:
//save data here
//In here you can save the value that has complete data

How to implement admin meta fields in Django 1.3 User profile?

from django.db import models
from django.contrib.auth.models import User
class MySiteProfile(models.Model):
# This is the only required field
user = models.ForeignKey(User, unique=True)
# The rest is completely up to you...
favorite_band = models.CharField(max_length=100, blank=True)
favorite_cheese = models.CharField(max_length=100, blank=True)
lucky_number = models.IntegerField()
The problem is that
User._meta.admin and MySiteProfile._meta.admin both return NoneType. I've dropped and recreated whole database, but no new fields appeared in admin panel; AUTH_PROFILE_MODULE is set.
There are a few ways you can make your MySiteProfile show up in the admin. One is to simply register your model with the admin, and it will show up under the app name it resides in.
Another is to unregister the contrib user from admin and instead load yours:
#admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_app.models import MySiteProfile
class MySiteProfileAdmin(UserAdmin):
def __init__(self,*args,**kwargs):
super(MySiteProfileAdmin).__init__(*args,**kwargs)
fields = list(UserAdmin.fieldsets[0][1]['fields'])
fields.append('favorite_band')
fields.append('favorite_cheese')
fields.append('lucky_number')
UserAdmin.fieldsets[0][1]['fields']=fields
admin.site.unregister(User)
admin.site.register(MySiteProfile, MySiteProfileAdmin)
There are quite a few articles around on this topic, but hope that helps you out.

Django User model fields at AdminModel

My purpose is to see at the admin site only user name, email and phone number.
I've create UserProfile by extending User model:
model.py
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
name = models.CharField(max_length=50, null=True,blank=True)
address = models.CharField(max_length=50, null=True,blank=True)
phone = models.CharField(max_length=20, null=True,blank=True)
country = models.CharField(max_length=20, null=True,blank=True)
state = models.CharField(max_length=20, null=True,blank=True)
zip = models.CharField(max_length=10, null=True,blank=True)
code = models.CharField(max_length=40, null=True)
def user_email(self):
return self.user.email
admin.py
from myApp.models import UserProfile
from django.contrib import admin
class UserProfileAdmin(admin.ModelAdmin):
fields = ('name','phone',)
list_display = ('name','user_email',)
admin.site.register(UserProfile, UserProfileAdmin)
so on the list_display it works, I can see only the columns I've chosen, but when I add 'user_email' ( fields = ('name','user_email', 'phone',) )to fields I get when I try to go to admin site:
'UserProfileAdmin.fields' refers to field 'user_email' that is missing from the form.
Fields on a related model use two underscores. Dunno if it'll work in the admin though.
list_display = ('name','user__email',)
Just because I recently used it and you maybe want this, too: If you wan't to add an inline admin to the "User" admin page in Django you can do this (at least in Django 1.3) by doing:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from models import UserProfile
class UserProfileInlineAdmin(admin.StackedInline):
model = UserProfile
class MyUserAdmin(UserAdmin):
inlines = [ UserProfileInlineAdmin ]
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
You can't put editable fields from a related model into an admin form, without using inlines. You can show the field as a readonly value: just add it to readonly_fields.