3

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>
Adithya
  • 63
  • 1
  • 8
T. Tosin
  • 31
  • 2
  • Please show how you try to register the same person as a student in the question. – Abdul Aziz Barkat Apr 21 '21 at 11:30
  • Hi, I've updated. Thanks. – T. Tosin Apr 21 '21 at 14:30
  • You use `CreateView` to create new instances of `Student`, etc. so when you specify a `cpf` you basically try to create a new `Person` instance with the same `cpf` giving you an error. I personally don't like multi-table inheritance much and prefer explicit one-to-one fields more. Anyway you can try something after referring this [question](https://stackoverflow.com/questions/4064808/django-model-inheritance-create-sub-instance-of-existing-instance-downcast) – Abdul Aziz Barkat Apr 21 '21 at 15:01

0 Answers0