我有一个小问题。
class Person(models.Model):
cpf = BRCPFField(**unique=true**)
class Student(Person):
new_field = ....
class Teacher(Student):
another_field = ...
class BoardMember(Person):
class Meta:
proxy = True
CPF = 美国 SSN。
一个人可以是学生,董事会成员,学生将来可以是老师。
当我用cpf注册一个人时,没关系。但是当我尝试将同一个人注册为学生时,由于唯一字段,python 显示错误。我该如何解决这个问题?
形式:
class PersonForm(forms.ModelForm):
class Meta:
model = Person
fields = '__all__'
class BoardMemberForm(PersonForm):
class Meta:
model = BoardMember
fields = '__all__'
class StudentForm(PersonForm):
class Meta:
model = Student
fields = '__all__'
意见:
class StudentCreate(LoginRequiredMixin, CreateView):
model = Student
form_class = StudentForm
login_url = reverse_lazy('users:login')
class BoardMember Create(LoginRequiredMixin, CreateView):
model = BoardMember
login_url = reverse_lazy('users:login')
form_class = BoardMemberForm
模板 Student_form:
<form class="row gx-3 gy-2 align-items-center needs-validation" method="post" action=""
role="form" enctype="multipart/form-data">
{% csrf_token %}
{{ form.media }}
<div class="row g-3">
<div class="col-md-3">
<div class="form-floating">
{{ form.cpf }}
<label for="id_cpf">CPF</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating">
{{ form.nome }}
<label for="id_nome">Nome</label>
</div>
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">Enviar</button>
</div>
</form>
模板 BoardMember_form:
<form class="row gx-3 gy-2 align-items-center needs-validation" method="post" action=""
role="form" enctype="multipart/form-data">
{% csrf_token %}
{{ form.media }}
<div class="row g-3">
<div class="col-md-3">
<div class="form-floating">
{{ form.cpf }}
<label for="id_cpf">CPF</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating">
{{ form.nome }}
<label for="id_nome">Nome</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating">
{{ form.function }}
<label for="id_nome">Function</label>
</div>
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">Enviar</button>
</div>
</form>