我在Django的register
表单和admin
页面中添加了新列。我可以使用该列进行注册,但是注册后,当我查看admin
页面时,用户没有应用该列。应该是grade*
选择域的寄存器字段,以及Employee
类,内部带有department
的变量,以将grade
从寄存器页保存到department
列到我的数据库。我做错了什么?
models.py
from django.db import models
from django.contrib.auth.models import User
from PIL import Image
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default='default.jpg', upload_to='media')
def __str__(self):
return f'{self.user.username} - プロファイル'
# recreate save function of django for specified save- resized
def save(self, *args, **kawrgs):
super().save(*args, **kawrgs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size) # resize
img.save(self.image.path) # save img
class Employee(models.Model): #HERE
user = models.OneToOneField(User, on_delete=models.CASCADE) #HERE
department = models.CharField(max_length=100)#HERE
def __str__(self):#HERE
return f'{self.user.username} - プロファイル' #HERE
def save(self, *args, **kawrgs): #HERE
super().save(*args, **kawrgs) #HERE
admin.py
from django.contrib import admin
from .models import Profile
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import Employee
from django.contrib.auth.models import User
admin.site.register(Profile)
# Define an inline admin descriptor for Employee model
# which acts a bit like a singleton
class EmployeeInline(admin.StackedInline): # HERE
model = Employee #HERE
can_delete = False #HERE
verbose_name_plural = 'employee' #HERE
# Define a new User admin
class UserAdmin(BaseUserAdmin): #HERE
inlines = (EmployeeInline, ) #HERE
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
我在forms.py中也有一栏用于注册表格
busho =[("1", "管理部"), ("2", "経理部"), ("3", "工事部")] #HERE
class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Eメール",
widget=forms.TextInput(attrs={'style': 'position: relative;left: 0px;'}))
first_name = forms.CharField(label = "名", max_length=10,
widget=forms.TextInput(attrs={'style': 'position: relative;left: 0px;'}))
last_name = forms.CharField(label = "性", max_length=10,
widget=forms.TextInput(attrs={'style': 'position: relative;left: 0px;'}))
username = forms.CharField(label = "ユーザー名", max_length=10,
widget=forms.TextInput(attrs={'style': 'position: relative;left: 0px;'}))
password1 = forms.CharField(label = "パスワード",
widget=forms.PasswordInput(attrs={'style': 'position: relative;left: 0px;'}))
password2 = forms.CharField(label = "パスワード確認",
widget=forms.PasswordInput(attrs={'style': 'position: relative;left: 0px;'}))
grade = forms.ChoiceField(label = "grade", choices=busho) #HERE
class Meta:
model = User
fields = ["username", "first_name", "last_name", "email", "password1", "password2", 'grade'] #HERE
def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
user.username = self.cleaned_data["username"]
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
user.email = self.cleaned_data["email"]
user.password1 = self.cleaned_data["password1"]
user.password2 = self.cleaned_data["password2"]
user.employee.department = self.cleaned_data["grade"] #HERE
if commit:
user.save()
return user