Link two unmanaged models via ForeignKey?

Hello,

I have two unmanaged models that I get from the database using inspectdb. Both models are based on views. Now the foreign key for cost_bearer from the PentaProjectMasterData model should be set in the PentaSystem model. How do I have to do that? I commented out a line. Does it work something like this?

This is what my code from models.py looks like.

from django.db import models

 

class PentaProjectMasterData(models.Model):

    project = models.CharField(max_length=10, blank=False, null=False)

    project_name = models.CharField(max_length=8000, blank=True, null=True)

    cost_bearer = models.CharField(max_length=6, blank=False, null=False, primary_key=True)

 

    class Meta:

        managed = False  # Created from a view. Don't remove.

        db_table = 'penta_project_master_data'

 

class PentaSystem(models.Model):

    system = models.CharField(max_length=4, blank=False, null=False, primary_key=True)

    system_name = models.CharField(max_length=30, blank=True, null=True)

    cost_bearer = models.CharField(max_length=6, blank=False, null=False)

 

    #cost_bearer = models.ForeignKey(PentaProjectMasterData, on_delete=models.CASCADE)

 

    class Meta:

        managed = False  # Created from a view. Don't remove.

        db_table = 'penta_system'

By the way, I’m a complete Django newbie and am currently trying to implement my first project.

Welcome @paddy-4483 !

The first thing to point out here is that the inspectdb is a starting point and not the ending point. It is expected that after running inspectdb that you will need to edit the models to adjust for things that weren’t picked up by it.

So having said that, the line that you have commented out looks right at first glance. (However, this means that you need to comment out the duplicate definition above it.)

What happens when you do this?

Hi Ken, thank you very much. I realize that the command is just the start. I just didn’t know how to set the foreign key.

I’ll test the commented out version.

I tested it. It works, but I still had to make a few small adjustments. The problem is that Django automatically appends “_id” to the FK. So I had to adjust the columns accordingly.

Now I have another problem. I’m trying to create a new ResponsiblePerson in the admin panel. But for the Cost bearer and System fields I get a) way too many values ​​in the dropdown and b) they are somehow chained.

This is what my models.py looks like:

from django.db import models

 

class ProjectMasterData(models.Model):

    project = models.CharField(max_length=10, blank=True, null=True)

    project_name = models.CharField(max_length=8000, blank=True, null=True)

    cost_bearer_id = models.CharField(max_length=6, blank=False, null=False)

    system_id = models.CharField(max_length=4, blank=False, null=False)

    system_name = models.CharField(max_length=30, blank=True, null=True)

    key_id = models.CharField(max_length=10, primary_key=True)

 

    class Meta:

        managed = False  # Created from a view. Don't remove.

        db_table = 'project_master_data'

 

    def __str__(self):

        return f"{self.cost_bearer_id} | {self.system_id}"

   

 

class ResponsiblePerson(models.Model):

    name = models.CharField(max_length=100, blank=False, null=False)

    department = models.CharField(max_length=100, blank=False, null=False)

   

    cost_bearer = models.ForeignKey(ProjectMasterData, on_delete=models.CASCADE, related_name='cost_bearer', db_constraint=False)

    system = models.ForeignKey(ProjectMasterData, on_delete=models.CASCADE, related_name='system', db_constraint=False)

 

    def __str__(self):

        return f"{self.department} | {self.name} | {self.cost_bearer} | {self.system}"

When I try to create a new entry in the admin panel, it looks like the following appears in both the dropdown for Cost bearer and System: 313759 | 0310. The value for Cost bearer is only “313759” and “0310” is actually the value for system. What am I doing wrong or how do I get it changed?