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

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.

Related

Count and store Number of logins in Django

I am new to Django framework. I am trying to record number of logins by a particular user(s) and display it in my Django panel. I went through the discussion here
But could not understand which code is to put in which file i mean what to put in models.py and signals.py or otherwise.
Please help.
create signals.py
from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver
from . models import Profile
#receiver(user_logged_in)
def user_logged(sender,request,user,**kwargs):
user.profile.log_count+=1
user.profile.save()
create Profile model, if exists add log_count field.
class Profile(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="profile")
log_count = models.IntegerField(default=0)

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

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)

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 manage fields created with django-primate in django-admin?

I use django-primate. I override standard user class with 3 new fields:
from primate.models import UserBase, UserMeta
from django.db import models
class CustomUser(UserBase):
__metaclass__ = UserMeta
name = models.CharField(max_length=500, default='Jon Deg')
jabber_id = models.CharField(max_length=150, default='jabber#jabber.org')
title = models.CharField(max_length=20, blank=True)
And, of course, add admin.py:
from primate.admin import UserAdminBase
from django.contrib import admin
from django.contrib.auth.models import User
class UserAdmin(UserAdminBase):
pass
admin.site.register(User, UserAdmin)
Now in django-admin I can manage only 'name' and some other standard auth fields. How can I add, for example, jabber_id to django-admin?

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.