django - add field to user profile admin form - python

With the given screenshot, I want to add a select dropdown to the user add page from another model that already exists on the system. how do i do this?
Here is my account/models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from entities.models import Company
from entities.models import Venue
class UserProfile(models.Model):
user = models.OneToOneField(User)
company = models.ForeignKey(Company)
venue = models.ForeignKey(Venue)
def __unicode__(self):
return 'Test'
and here is my account/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from entities.models import Company
from entities.models import Venue
class CompanyInline(admin.StackedInline):
model = Company
class VenueInline(admin.StackedInline):
model = Venue
class MyUserAdmin(UserAdmin):
inlines = [
CompanyInline,
VenueInline,
]
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
now when I add the above code and return to the user add page, I have this error:
The Company and Venue models are both populated with data.
Any help would be greatly appreciated.
Thanks,
Mark

Arg, this happens every time I post something up. I give the code another look-through and find the problem.
Anyways, for the other unfortunates having the same problem, I just changed my account/admin.py to the following
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from account.models import UserProfile
class UserProfileInline(admin.TabularInline):
model = UserProfile
class MyUserAdmin(UserAdmin):
inlines = [
UserProfileInline,
]
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

Related

Import-export module works not with User model for me in Django

I am trying to import new users to original User model form the Django admin site. I used to add the users one-by-one manually to the model but now I need to import lots of users and I need a solution to bulk import. I tried the code below, but I have an error:
django.contrib.admin.sites.NotRegistered: The model User is not registered
Please help me to solve this.
admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from import_export.admin import ImportExportModelAdmin
from import_export import resources
class UserResource(resources.ModelResource):
class Meta:
model = User
fields = ('id','username','first_name', 'last_name', 'email' 'password1', 'password2')
class UserAdmin(ImportExportModelAdmin):
list_display = ('id','username','first_name', 'last_name', 'email')
resource_class = UserResource
pass
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

How to add a custom field to Django user module?

This is the image of the default Django User fields.
models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Visitor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.IntegerField()
admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from accounts.models import Visitor
class VisitorInline(admin.StackedInline):
model = Visitor
can_delete = False
verbose_name_plural = "visitor"
class UserAdmin(BaseUserAdmin):
inlines = (VisitorInline,)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
This image has the default Django user fields such as username, email address, first name and last name. I want to add a custom field such as a phone number to the existing fields. How can I achieve this? Thank you!

Adding phonenumber field to signup form Django

When I enter the details in this form and press the submit button , I don't see the values of phoneno and otp getting saved in the database .The fields phone number and otp are not shown at all .
SEE image only username is saved and the otp and phone number fields are not displayed nor saved
This is my signup/forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
# Create your models here.
class allusers1(UserCreationForm):
phoneno1=forms.CharField(label = "phonewa",max_length=10)
otp1=forms.IntegerField(label="OTPP",required=False)
class Meta:
model=User
fields=(
'username',
'password',
'phoneno1',
'otp1',
)
def save(self,commit=True):
user=super(allusers1,self).save(commit=False)
user.username1=self.cleaned_data['username']
user.password1=self.cleaned_data['password']
user.phoneno1=self.cleaned_data['phoneno1']
user.otp1=self.cleaned_data['otp1']
if commit:
user.save()
return user
This is mysignup/forms.py
from django.shortcuts import render
from .forms import allusers1
def signup(request):
form1=allusers1(request.POST or None)
if form1.is_valid():
form1.save()
context = {
"form1": form1,
}
return render(request, "signup.html",context)
Your User model is the default django.contrib.auth.models.User that you import in the second line. This model has predefined fields. otp1and phoneno1 are not amongst them as you can see from the docs. So when you save a Userinstance, these attributes are simply ignored.
So you have to extend the User model like described in the docs (Django 2.0).
# models.py
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
otp1 = models.IntegerField(null=True)
phoneno1 = models.CharField(max_length=10)
# settings.py
settings.AUTH_USER_MODEL = 'myapp.User'
# admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)
You won't need a special form then for the Django admin. Your own model will inherit everything Django's User model brings with it, plus your own fields / methods.

Django extending user model tutorial isn't work for me

I've use this tutorial and do exactly what they do:
https://docs.djangoproject.com/ja/1.9/topics/auth/customizing/#extending-the-existing-user-model
my model.py:
from django.db import models
from django.contrib.auth.models import User
from datetime import datetime
class Chess(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
last_activity = models.DateTimeField(default=datetime(1970,1,1,))
is_avaliable = models.BooleanField(default=False,)
in_progress = models.BooleanField(default=False,)
my admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from chess_tournament.models import Chess
class ChessInline(admin.StackedInline):
model = Chess
can_delete = False
verbose_name_plural = 'Chess'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (ChessInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
in managaer.py shell:
from django.contrib.auth.models import User
u = User.objects.get(pk=1)
u.username # return 'admin' it's work
u.chess.last_activity # return ERROR (described below)
AttrinbuteError: 'User' object has no attribute 'chess'
but in django admin panel this field are available and works
Please help to figure it out coz I already spent 4 hours for it...
You need to update your Chess model and add related_name to the user property.
class Chess(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="chess_board")
# Other Stuff
Now you can easily access last_activity or any other property:
u.chess_board.last_activity
Hope it helps.
Could it possible that you didn't register Chess model?
Try add admin.site.register(Chess, ChessAdmin) at the bottom of admin.py. Of course you might have to create a simple ChessAdmin for display first.

How to add some extra fields to the user in django-cms? (in django admin panel)

I would like to add some extra fields to user in django-cms (in django admin panel). How to do this in the simplest way?
Need to add two fields user bio and image. And can i use this in frontend to show a page with all the user info?
From the django docs
Create you custom user model like this
from django.contrib.auth.models import User
class Employee(models.Model):
user = models.OneToOneField(User)
department = models.CharField(max_length=100)
Change the admin file like this
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Employee
# 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(UserAdmin):
inlines = (EmployeeInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
OR
Another option is to define a custom user model. For more details please visit https://docs.djangoproject.com/en/1.8/topics/auth/customizing/#a-full-example
Taken from Django documentation:
The simplest way would be to create what is often called a profile model. So for your example you would create something like
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField()
image = models.ImageField()
Then, for seeing this in the admin panel, you would reregister admin for the User
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_user_profile_app.models import Profile
class UserProfileInline(admin.StackedInline):
model = Profile
can_delete = False
verbose_name_plural = 'profile'
# Define a new User admin
class UserAdmin(UserAdmin):
inlines = (UserProfileInline, )
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
As far as showing this in frontend, you can use User as well as Profile the same way you would use other Django models.

Categories