I'm with a little problem.
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 = USA SSN.
A person can be a student, a board member and a student can be a teacher in future.
When I register a person with a cpf, it's ok. But when I try to register the same person as a student, python show me a error because of the unique field. How can I solve this?
Forms:
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__'
Views:
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
Template 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>
Template 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>