Python Django heritage with Unique attribute

Asked

Viewed 27 times

0

I’m in a little trouble.

class Pessoa(models.Model): 
cpf = BRCPFField(**unique=true**)

class Aluno(Pessoa):
 novo_atributo = ....

class Instrutor(Aluno):
 outro_atributo = ...

class Gestor(Pessoa):
class Meta: 
   proxy = True

A person can be a student, a manager and a student can become an instructor in the future.

When I register a student with an X-Number, 100%. But when I try to register this same student(person) with the CPF X as manager, it is an error because the CPF is as Unique. How can I solve this?

I have tried to leave the Pessoa class as abstract, but what I want is to leave the table unique person, to avoid duplicity. Because the same person can be a manager, student and instructor. I thought about the inheritance, because it helps a lot in relation to the bank.

Forms:

class PessoaForm(forms.ModelForm):
    class Meta:
    model = Pessoa
    fields = '__all__'


class GestorForm(PessoaForm):
   class Meta:
        model = Gestor
        fields = '__all__'


class AlunoForm(PessoaForm):
   class Meta:
        model = Aluno
        fields = '__all__'

Views:

class AlunoCreate(LoginRequiredMixin, CreateView):
    model = Aluno
    form_class = AlunoForm
    login_url = reverse_lazy('usuarios:login')


class GestorCreate(LoginRequiredMixin, CreateView):
    model = Gestor
    login_url = reverse_lazy('usuarios:login')
    form_class = GestorForm

Template Aluno_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 Gestor_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.funcao }}
                <label for="id_nome">Função</label>
            </div>
        </div>            
    </div>           
    <div class="col-auto">
        <button type="submit" class="btn btn-primary mb-2">Enviar</button>
    </div>
</form>
  • Unique no dj sera only 1 Cpf can not have another with the same numeral can make relationship 1 to 1, 1 a person can be one.

  • So that’s the idea, the problem is how to do that? because in the form, when I try to register an existing student as a board member, the system accuses error on account of the existing Cpf.

  • whereas a student may be an instructor and eventually an instructor was a student, I added a foreign key. aluno_futuro_instructor = models.Foreignkey(Student, related_name='vacancy', on_delete=models.CASCADE). ... in the instructor model table

1 answer

0

Do it in a way that when adding a student(person) with the CPF X as manager, that student Cpf is deleted before inserting in manager. Or leave this number as a foreign key, to be called in other positions.

EX:

class Pessoa(models.Model):
    nome = charfield....
    cpf_pessoa = models.charfield(max_length=11,unique=True)

class Aluno(models.Model):
    aluno = models.ForeignKey('Pessoa', on_delete=models.CASCADE)

class Gestor(models.Model):
    gestor = models.ForeignKey('Pessoa', on_delete=models.CASCADE)

So when Voce assign anyone to manager, it will already inherit the CPF...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.