How to use objects in Django

Asked

Viewed 144 times

-1

I have to create models based on the following UML: inserir a descrição da imagem aqui

I actually made the code of Experience that way:

class Experiencia(Base):
    ACADEMICA = 'AC'
    PROFISSIONAL = 'PR'
    TIPO_CHOICES = [
        (ACADEMICA, 'Acadêmica'),
        (PROFISSIONAL, 'Profissional'),
    ]
    candidato = models.ForeignKey(Candidato, on_delete=models.CASCADE)
    tipo = models.CharField(choices=TIPO_CHOICES, max_length=50)
    data_inicio = models.DateField()
    data_fim = models.DateField()
    observacao = models.TextField(max_length=400)

I am in doubt on how to place the objects of Academica and of Profissional in choices, because what you currently have are just the strings. The question to Tipo_curso, how to place it to be a Academica. And if there was any way to use polymorphism in that.

  • You are thinking of: I want to record 'Academic Experience' or 'Professional Experience' or use the relation 'many for many' (Academic and Professional) the same is in the diagram?

  • @Carloscortez Each experience can be just one: Academic or professional. And in another class I have a field that can have several experiences. What I want to know is how to implement for Experience Option reference professional or academic. That is, when I indicate that the experience is academic, how will I know this data? Ex: institution, course. I have the same question for Academica and Tipocurso.

  • I confess q would also like to know how to make this dynamic change using only python/Django. What I have come across is manipulation via Java script in the Forms/views, never directly in the models and in the administrative area. I will try to overwrite the templates and if I get anything, I publish

1 answer

2

I prefer to use the form below as it gives a visual separation.

update

Libraries required

from django.db import models
from django.utils.translation import gettext_lazy as _

end of update

class Experiencia(models.Model):
    class ExperienciaChoices(models.TextChoices):
        AC = 'AC', _('Acadêmica')
        PR = 'PR', _('Profissional')

    candidato = models.ForeignKey(Candidato, on_delete=models.CASCADE)
    tipo = models.CharField(max_length=2, choices=ExperienciaChoices.choices, default=ExperienciaChoices.AC)
    data_inicio = models.DateField()
    data_fim = models.DateField()
    observacao = models.TextField(max_length=400)

    objects = ExperienciaManager()

    def __str__(self):
        return self.candidato

    class Meta:
        ordering = ['candidato']
        verbose_name_plural = 'candidatos'


class ExperienciaManager(models.Manager):
    def conta_experiencia(self, keyword):
        return self.filter(tipo__icontains=keyword).count()

Note Using the manager allows you to create methods that can be used with the objects as an example below

total_de_academica = Experiencia.objects.conta_experiencia(‘AC’) 

Note 2 Obviously I am not taking into account if this is a valid business rule for your case. Just showing the functionality.

I hope it helps

  • My biggest doubt is more related to access to academic and professional objects. I don’t see how it would be possible to access their data after choosing a

  • Experiencia.objects.all() returns a "list" of objects of the type Experiencia. Then just access the attributes... was that?

  • No, Experiencia.objects.all() will return me candidate, type, etc. I wondered how to get The Academic Experience for example. I have an object of type Experience, that has as Academica type, as access the data of Academica? course, institution etc. Note that the Experience Schools point to the Academic class. The same happens from academics to courses.

  • Paulo, give me a light! Haha where would I put this 'total_de-academia'? In the shell, it is possible to access easily and without difficulty but in the models (for example) it displays message of q the class does not offer the Objects option

  • I don’t quite understand your question. Usually you will access the method in a view or in a business layer, usually called services.py, but it can have any name, some call facade.py...

  • @Paulomarques I arrived at the problem that I thought was coming, I asked him a question, see if you can understand it better: https://answall.com/questions/493218/como-mudar-inlines-de-acordo-com-umoption-no-django-admin

Show 1 more comment

Browser other questions tagged

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