我在Django中有一个来自User模型的扩展表,是userinfo。我创建了具有以下字段列表的表单:“ first_name”,“ last_name”,“ date_of_birth”,“ gender”,“ address”,“ phone_number”,“ email”。我在forms.py
的Meta类中遇到了模型问题UserInfo模型
class UserInfor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
date_of_birth = models.DateField()
gender = models.CharField(max_length=10)
address = models.CharField(max_length=255)
phone_number = models.CharField(max_length=10)
avatar = models.ImageField(upload_to='profile-image', blank=True)
def __str__(self):
return self.user.username
forms.py
from django import forms
from django.forms import ModelForm
from .models import UserInfor, User
class ProfileForm(ModelForm):
class Meta:
model = User, UserInfor
fields = ['first_name', 'last_name', 'date_of_birth', 'gender', 'address', 'phone_number', 'email']
widgets = {
'first_name': forms.TextInput(attrs={'type': 'text', 'placeholder': 'First Name', 'class': 'form-control input-md'}),
'last_name': forms.TextInput(attrs={'type': 'text', 'placeholder': 'Last Name', 'class': ' form-control input-md'}),
'date_of_birth': forms.DateInput(attrs={'placeholder': 'Date of Birth', 'class': 'form-control input-md'}),
'gender': forms.RadioSelect(choices=[('nam', 'Nam'), ('nữ', 'Nữ'), ('khác', 'Khác')]),
'address': forms.TextInput(attrs={'type': 'text', 'placeholder': 'Address', 'class': 'form-control input-md'}),
'phone_number': forms.TextInput(attrs={'placeholder': 'Phone Number', 'class': 'form-control input-md'}),
'email': forms.EmailInput(attrs={'placeholder': 'Email', 'class': 'form-control input-md'}),
}
labels = {
'first_name': "Họ",
'last_name': "Tên",
'date_of_birth': "Ngày sinh",
'gender': "Giới tính",
'address': "Địa chỉ",
'phone_number': "Số điện thoại",
'email': "Email",
}