I wonder if it is possible to join two tables in the Django Framework

Asked

Viewed 444 times

1

Hello friends, I’m having a slight difficulty in how to generate a third Model within Django, currently I have two tables Training and Entity would like to know if it would be possible to create another app and join these two tables.

My code is :

Folder Training/models:

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from phone_field import PhoneField
from localflavor.br.forms import BRZipCodeField
from datetime import time


# Create your models here.

class Treinamento(models.Model):
    id = models.AutoField(primary_key=True)
    nome_treinamento = models.CharField(max_length=50)
    categoria = models.CharField(max_length = 50)
    conteudo =  models.TextField()
    requisito = models.CharField(max_length=50)
    recursos = models.CharField(max_length=50)
    carga_horaria = models.TimeField(auto_now=False, auto_now_add=False)
    tipo = models.CharField(max_length=50)
    local = models.CharField(max_length=50)

    def __str__(self):
        return self.nome_treinamento

Entity folder/models

from django.db import models
from django.core.validators import MaxValueValidator, MinValueValidator
from phone_field import PhoneField
from datetime import time


# Create your models here.
class Entidade(models.Model):
    id = models.AutoField(primary_key=True)
    nome_entidade = models.CharField(max_length=70)
    razao_social = models.CharField(max_length=70)
    codigo_sap = models.PositiveIntegerField(default=0, blank=False)
    pessoa_contato = models.CharField(max_length=20)
    email = models.EmailField(max_length=254, blank=False, unique=True, error_messages={'required': 'Porfavor digite seu e-mail.', 'unique': 'Já existe esse e-mail cadastrado.'},)
    phone = PhoneField(blank=True, help_text='Seu numero de telefone')
    cep = models.PositiveIntegerField(default=0,blank=False)
    endereço = models.CharField(max_length=100)
    cidade = models.CharField(max_length=100)
    estado = models.CharField(max_length=100)
    data_registro = models.DateTimeField(blank=True)

    def __str__(self):
        return self.nome_entidade
  • Explain better, the way q is seems n make sense, what would be the goal? What do you want as a result? P/q join the two?

  • Hello Sidon, my goal is to create an application that optimizes an excel table and for that I need 2 models one for the training that the person will give and one for the person who will do the training that I named as an entity, having this in view wanted to know how I can generate a 3 table with the junction of the fields of these two tables, if I can import a model from one app in another app of my project finally what solution exists for this?

  • Yes you can import the templates between the app,: from app.models import mymodel, now... "join" the tables? I cannot see the reason, with the two tables you can extract the information from the two in any way you wish/need, p/ create another?

  • That, I’ll show you I have the fields in the entity table that would be my client: [ Id_entity |Name| Social Reason | SAP Code| Person Contact | Email | Phone | Address | ZIP Code | City | State| Date Registration] And I have the fields in my table Training : [Id_training | Training | Category | Content| Requirements| Resources | Hourly Load | Type | Local] And my third serious table intersects the two with the event name: Id_event | Registration Training | Registration Entity | Value | Date | Query | Date Realized | Registration Quotation |

  • in an ORM database I would have to make a views with the INTERSECT of the two tables to generate a report. but I’m wanting to do this in Django which is a MVC database is postssivel?

  • Review your concepts, a ORM is not a database type, a ORM (Object Relational Mapping) is a mechanism to map a non-OO database into OO classes and yes, Django uses a ORM for this.

  • Okay, thanks for the help.

  • @Linikeroliveira what do you mean join? As was said in another comment you can import other tables from other apps normally, this junction would be in which app? training or entity? so you can create another class and inherit these two but I don’t think it would be cool since some fields might have conflicts

  • @Linikeroliveira the id field does not need to be placed, Django already does it for you ;)

  • @Daviwesley, I started all over again my project, now I will create a table within a single Model, but I have a question regarding where I can show the junction of two fields of different tables ( because I am using Foreing Key but it gives several errors) the view I need to do in the controller?

  • @Linikeroliveira all right? then, you can create an attribute in your class that calls some property some model and make the logic you want, see this link

  • @David Wesley thank you so much for the guidance, I had some ideas and I did everything within one app. thank you very much!

Show 7 more comments

1 answer

-1

Browser other questions tagged

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