How can I connect multiple model tables Django for data import export?
I have three 3 Django model tables.
They are kind of connected to each other so that each row, each line from one model table corresponds to the same from another model table.
You could say that I'm just splitting one large model table into three 3 medium small ones.
And each row, line from one model table is equal to belongs to another.
Rows, rows from 3 tables - one row from 3 tables - these are the parameters of one object.
How can I somehow connect these three 3 model tables? I'll be glad to any help. I want to save and upload data simultaneously to 3 model tables through the form.
How can I somehow load and receive data.
How can I connect multiple model tables for data import export? How can I connect multiple model tables for data import export as if it were one large model table - merged from 3 model tables?
from django.db import models
class Musician_1(models.Model):
first_name_1 = models.CharField(max_length=50)
last_name_1 = models.CharField(max_length=50)
instrument_1 = models.CharField(max_length=100)
class Musician_2(models.Model):
first_name_2 = models.CharField(max_length=50)
last_name_2 = models.CharField(max_length=50)
instrument_2 = models.CharField(max_length=100)
class Musician_3(models.Model):
first_name_3 = models.CharField(max_length=50)
last_name_3 = models.CharField(max_length=50)
instrument_3 = models.CharField(max_length=100)
If you want to make a model about multiple musician, you just need one model and you simply call it using some query.
For example;
from django.db import models
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
and after that you simply create an object per musician:
from models.py import Musician
musician_1 = Musician(first_name_1, last_name_1, instrument_1)
musician_2 = Musician(first_name_2, last_name_2, instrument_2)
musician_3 = Musician(first_name_3, last_name_3, instrument_3)
musician_1.save()
musician_2.save()
musician_3.save()
but if they're just example to "connect" models you can use ManyToMany, ForeignKey, or OneToOne
if you need to exclusively "connect" two:
from django.db import models
class Model1 (models.Model):
thing = models.CharField()
model_2 = models.OneToOne(Model2, on_delete=models.CASCADE, related_name='2model')
class Model1 (models.Model):
thing = models.CharField()
model_1 = models.OneToOne(Model1, on_delete=models.CASCADE, related_name= "1model")
you can continue like that for the other but ForeignKey is for one models to many models and ManyToMany is as it says