Hello everyone,
I’m now making quite good progress with my first Django application. So far, the data model has worked as intended. But now I have to integrate data from another database. I don’t want to continue working with this data, it should only be displayed in the frontend.
Here is some basic information about the setup.
Django DB: Server01, DB Name = ‘Django’
Other data DB: Server02, DB Name = ‘Data’
Both DB servers are SQL Server from Microsoft. What is the best way to integrate the data of the other DB now?
At the moment, I have simply created a model for my third-party database table in the models.py. I can empty and fill them by ETL. But this is certainly not the cleanest way.
Specifically, this is about linking purchasing positions with responsibilities. The join must be done via the cost_bearer, department, and system fields between the Resposibility and BottleneckPart models.
This is what my models.py file looks like.
from django.db import models
# Create your models here.
class Project(models.Model):
cost_bearer = models.CharField(max_length=6, primary_key=True, verbose_name='Kostenträger')
description = models.CharField(max_length=50, unique=True, verbose_name='Bezeichnung')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Projekt'
verbose_name_plural = 'Projekte'
def __str__(self):
return f'{self.cost_bearer} {self.description}'
class System(models.Model):
number = models.CharField(max_length=4, primary_key=True, verbose_name='Nummer')
description = models.CharField(max_length=100, verbose_name='Bezeichnung')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Baugruppe'
verbose_name_plural = 'Baugruppen'
def __str__(self):
return f'{self.number} {self.description}'
class Department(models.Model):
name = models.CharField(max_length=50, unique=True, verbose_name='Name')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Abteilung'
verbose_name_plural = 'Abteilungen'
def __str__(self):
return self.name
class Team(models.Model):
department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='Abteilung')
name = models.CharField(max_length=50, unique=True, verbose_name='Name')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Team'
verbose_name_plural = 'Teams'
def __str__(self):
return self.name
class Employee(models.Model):
team = models.ManyToManyField(Team, verbose_name='Team')
user = models.OneToOneField('auth.User', on_delete=models.CASCADE, verbose_name='Benutzer')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Mitarbeiter'
verbose_name_plural = 'Mitarbeiter'
def __str__(self):
return f'{self.user.first_name} {self.user.last_name}'
class Responsibility(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='Projekt')
system = models.ForeignKey(System, on_delete=models.CASCADE, verbose_name='Baugruppe')
team = models.ForeignKey(Team, on_delete=models.CASCADE, verbose_name='Team')
employee = models.ForeignKey(Employee, on_delete=models.SET_NULL, blank=True, null=True, verbose_name='Mitarbeiter')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Verantwortlichkeit'
verbose_name_plural = 'Verantwortlichkeiten'
constraints = [
models.UniqueConstraint(fields=['project', 'system', 'team', 'employee'], name='uq_project_system_team_employee')
]
def __str__(self):
return f'{self.project} | {self.system} | {self.team} | {self.employee}'
class Problem(models.Model):
responsibility = models.ForeignKey(Responsibility, on_delete=models.CASCADE, verbose_name='Verantwortlichkeit')
summary = models.CharField(max_length=150, verbose_name='Zusammenfassung')
description = models.TextField(verbose_name='Beschreibung')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Problem'
verbose_name_plural = 'Probleme'
def __str__(self):
return f'{self.responsibility} | {self.summary}'
class Solution(models.Model):
problem = models.OneToOneField(Problem, on_delete=models.CASCADE, verbose_name='Problem')
summary = models.CharField(max_length=150, verbose_name='Zusammenfassung')
description = models.TextField(verbose_name='Beschreibung')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Lösung'
verbose_name_plural = 'Lösungen'
def __str__(self):
return f'{self.problem} | {self.summary}'
class Status(models.Model):
COLORS = (
('Rot', 'Rot'),
('Gelb', 'Gelb'),
('Grün', 'Grün'),
)
responsibility = models.OneToOneField(Responsibility, on_delete=models.CASCADE, unique=True, verbose_name='Verantwortlichkeit')
traffic_light = models.CharField(max_length=4, choices=COLORS, default='Grün', verbose_name='Ampel')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Status'
verbose_name_plural = 'Status'
def __str__(self):
return f'{self.responsibility} | {self.traffic_light}'
class BottleneckPart(models.Model):
cost_bearer = models.ForeignKey(Project, on_delete=models.CASCADE, verbose_name='Kostenträger')
department = models.ForeignKey(Department, on_delete=models.CASCADE, verbose_name='Abteilung')
system = models.ForeignKey(System, on_delete=models.CASCADE, verbose_name='Baugruppe')
feedback_number = models.IntegerField(verbose_name='Rückmeldenummer')
feedback_position_number = models.CharField(max_length=6, verbose_name='Rückmeldepositionsnummer')
description_head = models.CharField(max_length=150, verbose_name='Kopfbezeichnung')
description = models.CharField(max_length=150, verbose_name='Bezeichnung')
po_position_status = models.CharField(max_length=50, blank=True, null=True, verbose_name='Bestellpositionsstatus')
inquiry_purchase_current = models.DateField(blank=True, null=True, verbose_name='Anfrage an KM IST')
inquiry_purchase_target = models.DateField(blank=True, null=True, verbose_name='Anfrage an KM SOLL')
purchase_requisition_planning_current = models.DateField(blank=True, null=True, verbose_name='Bestellanforderung an TP IST')
purchase_requisition_planning_target = models.DateField(blank=True, null=True, verbose_name='Bestellanforderung an TP SOLL')
po_finished_current = models.DateField(blank=True, null=True, verbose_name='Bestellung fertig IST')
po_finished_target = models.DateField(blank=True, null=True, verbose_name='Bestellung fertig SOLL')
appointment_document = models.CharField(max_length=50, blank=True, null=True, verbose_name='TU')
appointment_document_date = models.DateField(blank=True, null=True, verbose_name='TU Termin')
delivery_time_current = models.IntegerField(blank=True, null=True, verbose_name='Lieferzeit AT')
delivery_date_target = models.DateField(blank=True, null=True, verbose_name='Liefertermin SOLL')
created_at = models.DateTimeField(auto_now_add=True, editable=False, blank=True, verbose_name='Erstelldatum')
updated_at = models.DateTimeField(auto_now=True, editable=False, blank=True, verbose_name='Änderungsdatum')
class Meta:
verbose_name = 'Engpassteil'
verbose_name_plural = 'Engpassteile'
I hope you can understand what I want to achieve. I would be very grateful for a few tips.
Best regards
gospat