This is my model. When i make migrations it is not showing up on my admin page.
class User(AbstractUser):
# TODO User will be associated with one or more chama accounts
id_number = models.IntegerField(default=0)
phone_number = models.IntegerField(default=0)
active_chama = models.ManyToManyField("Chama")
You will have to set the AUTH_USER_MODEL setting in your settings.py if you use the AbstractUser model to create a custom one.
AUTH_USER_MODEL = "myApp.User"
Here is the relevant part in the docs
Edit:
Like stated in the comments you will have to register your custom model in your custom user apps admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)
Related
I am having two models projects and User in projects the User is related like shown below
models.py:
class project(models.Model):
user=models.OneToOneField(User,on_delete=models.CASCADE)
room = models.ForeignKey(room,on_delete=models.CASCADE)
goal = models.ManyToManyField(goal)
design = models.ManyToManyField(design)
furniture = models.ForeignKey(furniture,on_delete=models.CASCADE)
created_at = models.DateTimeField(default=datetime.now)
updated_at = models.DateTimeField(default=datetime.now)
Now here I want to display the extra column as projects in user page in django admin for every user when I click on that it should take to particular project detail page of that user
Screenshots:
This is the user list page
This is the project list page
This is the project detail page
admin.py:
from django.contrib import admin
from .models import project
class ProjectAdmin(admin.ModelAdmin):
readonly_fields = ('user','room','goal','design','furniture','created_at','updated_at')
admin.site.register(project,ProjectAdmin)
Please help me out Thanks in advance
You would need to write a custom admin for your User:
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.utils.safestring import mark_safe
UserModel = get_user_model()
admin.site.unregister(UserModel)
#admin.register(UserModel)
class CustomUserAdmin(UserAdmin):
list_display = (
'username', 'email', 'first_name', 'last_name', 'user_project'
)
def user_project(self, obj):
url = '/admin/modsy/project/{}/change/'.format(obj.project.pk)
return mark_safe('view project'.format(url))
I am trying to enable django import export on the django user model.
I have tried defining a model admin class, unregistering the user model and then registering the new user admin class. But it doesn't work.
my admin.py looks like this -
from django.contrib.auth.admin import UserAdmin as BaseAdmin
from import_export.admin import ImportExportModelAdmin
from import_export import resources
class UserResource(resources.ModelResource):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
class UserAdmin(BaseAdmin, ImportExportModelAdmin):
resource_class = UserResource
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
I want to know how can I achieve this? Is there some other way I can apply django import export on the user model?
Your code is working. You just need to import the User model:
from django.contrib.auth.models import User
Django 1.10.2
Studying customization of the User model.
Trying to follow the documentaion https://docs.djangoproject.com/en/1.10/topics/auth/customizing/#extending-the-existing-user-model
I wish to store that department and show it in the admin.
The problem is that department doesn't appear.
I played with fieldsets and list_display, but failed.
Could you help me understand how to show the department in the admin?
admin.py
from django.contrib.auth.models import User
from django.db import models
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
department = models.CharField(max_length=100)
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline):
model = Employee
can_delete = False
verbose_name_plural = 'employee'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (EmployeeInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Every user has (should have) a UserProfile object, and every UserProfile can have Locations against it (foreign key in Location). I want to show these Locations (and allow editing/adding/deleting them) in the User view in the admin site. Nested inlines aren't possible, so I'd like to add a LocationInline to the User page, which I'm unsure how to do.
models.py
from django.db import models
from registration.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
# ...
class Location(models.Model):
owner = models.ForeignKey(UserProfile)
# Address and stuff
admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from main.models import UserProfile, Location
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
class UserProfileInline(admin.StackedInline):
model = UserProfile
max_num = 1
can_delete = False
class LocationInline(admin.TabularInline):
model = Location
extra = 1
class UserAdmin(AuthUserAdmin):
inlines = [UserProfileInline, LocationInline]
# Obviously doesn't work, because Location is from UserProfile, not User
# How can I make it use user.profile instead?
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
I am new to Django and I have been trying this for weeks, but could not find a way to solve this problem.
I want to store additional information like user mobile number, bank name, bank account. And want to store the mobile number while user registers and wants user to login with either (mobile number and password) or (email and password).
This is my UserProfile model
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.models import AbstractUser
# Create your models here.
class UserProfile(AbstractUser):
user_mobile = models.IntegerField(max_length=10, null=True)
user_bank_name=models.CharField(max_length=100,null=True)
user_bank_account_number=models.CharField(max_length=50, null=True)
user_bank_ifsc_code = models.CharField(max_length=30,null=True)
user_byt_balance = models.IntegerField(max_length=20, null=True)
And this is my forms.py
from django import forms
from django.contrib.auth.models import User # fill in custom user info then save it
from django.contrib.auth.forms import UserCreationForm
from models import UserProfile
from django.contrib.auth import get_user_model
class MyRegistrationForm(UserCreationForm):
email = forms.EmailField(required = True)
mobile = forms.IntegerField(required=True)
class Meta:
model = UserProfile
fields = ('username', 'email', 'password1', 'password2','mobile' )
def save(self,commit = False):
user = super(MyRegistrationForm, self).save(commit = False)
user.email = self.cleaned_data['email']
user.user_mobile = self.cleaned_data['mobile']
user.set_password(self.cleaned_data["password1"])
user_default = User.objects.create_user(self.cleaned_data['username'],
self.cleaned_data['email'],
self.cleaned_data['password1'])
user_default.save()
if commit:
user.save()
return user
In my settings.py I have included
AUTH_USER_MODEL = "registration.UserProfile"
admin.py of my app is
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from models import UserProfile
class UserProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'userprofile'
class UserProfileAdmin(UserAdmin):
inlines = (UserProfileInline, )
admin.site.register(UserProfile, UserProfileAdmin)
While adding the user from admin I get this error
Exception at /admin/registration/userprofile/1/
<class 'registration.models.UserProfile'> has no ForeignKey to <class 'registration.models.UserProfile'>
Can someone help me with this or point out to the full working exapmle, I have seen Django documentation but didn't find any luck. Or if there is another way to do this.
Thanks in advance
Edit 1:
While registering from the registration form I'm also getting this error
DatabaseError at /register
(1146, "Table 'django_auth_db.auth_user' doesn't exist")
You have confused yourself a bit here. The idea of subclassing AbstractUser - and defining AUTH_USER_MODEL as your subclass - is that the new model completely replaces auth.models.User. You shouldn't be importing the original User at all, and you certainly should be calling User.objects.create_user(): your new model's manager now has its own create_user method.
Because of this, there's no reason to muck about with inline admins. Your UserProfile should be registered in the admin using the existing django.contrib.auth.admin.UserAdmin class.
Inlines forms assume that you have a Generic ForeignKey on your model, in this case, the UserProfileAdmin expect a Generic ForeignKey of the UserProfile, that does not exists. Try to do a regular Model Admin, like:
class UserProfileAdmin(admin.ModelAdmin):
can_delete = False
verbose_name_plural = 'userprofile'
admin.site.register(UserProfile, UserProfileAdmin)