2017-06-21 105 views
0

我正在使用legay數據庫,因此我必須設置managed = False,這意味着我無法更新數據庫模式。Django模型 - 如何定義外鍵引用另一個表?

我想要做的是根據項目ID選擇分支。理想情況下,在分支表中它應該有一個project_id作爲外鍵,但以前的系統設計是另一個表(branches_projects)存儲這種關係。

我已經能夠解決一些使用https://docs.djangoproject.com/en/1.11/topics/db/sql/#django.db.models.Manager.raw的問題。 raw()會返回一個RawQuerySet,這並不理想。

我想知道是否有一種方法可以讓我在我的分支表中定義一個外鍵,這是project_id,但是引用/鏈接到branches_projects表?

class Branches(models.Model): 
    name = models.CharField(max_length=128) 
    branchpoint_str = models.CharField(max_length=255) 
    dev_lead_id = models.IntegerField(blank=True, null=True) 
    source = models.CharField(max_length=255) 
    state = models.CharField(max_length=255) 
    kind = models.CharField(max_length=255) 
    desc = models.TextField(blank=True, null=True) 
    approved = models.IntegerField() 
    for_customer = models.IntegerField() 
    deactivated_at = models.DateTimeField(blank=True, null=True) 
    created_at = models.DateTimeField(blank=True, null=True) 
    updated_at = models.DateTimeField(blank=True, null=True) 
    codb_id = models.IntegerField(blank=True, null=True) 
    pm_lead_id = models.IntegerField(blank=True, null=True) 
    version = models.CharField(max_length=20, blank=True, null=True) 
    path_id = models.IntegerField(blank=True, null=True) 
    branchpoint_type = models.CharField(max_length=255, blank=True, null=True) 
    branchpoint_id = models.IntegerField(blank=True, null=True) 

    class Meta: 
     managed = False 
     db_table = 'branches' 
     verbose_name_plural = 'Branches' 

class Projects(models.Model): 
     id = models.AutoField(primary_key=True) 
     name = models.CharField(max_length=40, primary_key=True) 
     status = models.CharField(max_length=255) 
     platform = models.CharField(max_length=255) 
     enabled = models.IntegerField() 
     path = models.CharField(max_length=128, blank=True, null=True) 
     tag_prefix = models.CharField(max_length=64, blank=True, null=True) 
     created_at = models.DateTimeField(blank=True, null=True) 
     updated_at = models.DateTimeField(blank=True, null=True) 
     codb_id = models.IntegerField(blank=True, null=True) 
     template = models.CharField(max_length=64, blank=True, null=True) 
     image_path = models.CharField(max_length=128, blank=True, null=True) 
     repository_id = models.IntegerField(blank=True, null=True) 
     number_scheme = models.CharField(max_length=32) 
     special_dir = models.CharField(max_length=32, blank=True, null=True) 
     project_family_id = models.IntegerField() 
     class Meta: 
      managed = False 
      db_table = 'projects' 
      verbose_name_plural = 'projects' 

class BranchesProjects(models.Model): 
    # project_id = models.IntegerField() 
    # branch_id = models.IntegerField() 
    project = models.ForeignKey(Projects, on_delete=models.CASCADE) 
    branch = models.ForeignKey(Branches, on_delete=models.CASCADE) 

    class Meta: 
     managed = False 
     db_table = 'branches_projects' 

我目前的原始查詢是這樣

SELECT br.id, br.name, br.created_at, br.updated_at, 
br.branchpoint_str, br.source 
FROM branches as br 
LEFT JOIN branches_projects as bp 
ON br.id = bp.branch_id 
WHERE bp.project_id = %s AND source != 'other' 
ORDER BY updated_at DESC 

回答

0

我終於得到它的工作......

在這個模型中,我用的是多對多如下:

class Branches(models.Model): 
name = models.CharField(max_length=128) 
project = models.ManyToManyField(Projects, 
    through='BranchesProjects', 
    related_name='project') 
branchpoint_str = models.CharField(max_length=255) 

然後爲了得到與我的原始sql相同的結果,我做了以下操作:

def get_sb(project_id): 
    result = Branches.objects.filter(
     project=Projects.objects.get(id=project_id).id, 
     ).exclude(source='other').order_by('-updated_at') 
    print result.query 
    return result 
相關問題