2017-10-10 35 views
0

我使用的是Django 1.11,为了将我的MySQL Join request转换为Django QuerySet语法,我想获得正确的语法。`MySQL JOIN`和Django之间的等效代码

我有3点不同的模式(以我的身份申请1种模式,在我的财政申请2型)如下:

#Identity.Individu 

class Individu(models.Model): 

    NumeroIdentification = models.CharField(max_length=30, null=True, verbose_name='Numero Identification physique', unique=True) 
    Civilite = models.CharField(max_length=12,choices=CHOIX_TITRE, verbose_name='Civilité') 
    NomJeuneFille = models.CharField(max_length=30, verbose_name='Nom de jeune fille', blank=True) 
    Nom = models.CharField(max_length=30, verbose_name='Nom de famille') 
    Prenom = models.CharField(max_length=30, verbose_name='Prénom(s)') 

#Fiscal.Profession 

class Profession (models.Model) : 

    Employe = models.ForeignKey(Individu, related_name='Individu', verbose_name='Individu', null=False, to_field='NumeroIdentification') 
    Entreprise = models.ForeignKey(Societe, related_name='Société', verbose_name='Société', null=False, to_field='NumeroIdentification') 
    NumeroImpot = models.CharField(max_length=30, null=True, verbose_name='Numéro Imposition', unique=True) 
    Profession = models.CharField(max_length=30, verbose_name='Profession') 

#Fiscal.Salaire 

class Salaire (models.Model) : 

    Employe = models.ForeignKey(Individu, related_name='Salaire_Individu', verbose_name='Salaire_Individu', null=False, to_field='NumeroIdentification') 
    Salaire_B_mensuel = models.IntegerField(verbose_name='Salaire Brut Mensuel') 
    Salaire_N_mensuel = models.IntegerField(verbose_name='Salaire Net Mensuel') 

与MySQL,我有这样的要求的正常工作:

SELECT * FROM Identity_individu i 
    INNER JOIN Fiscal_profession p ON i.NumeroIdentification = p.Employe_id 
    INNER JOIN Fiscal_salaire s on i.NumeroIdentification = s.Employe_id; 

我的事情我必须使用prefetch_related,我写这个(第一步,两个表之间的连接):

@login_required 
def FiscalReporting(request) : 

    Contribuable = Individu.objects.prefetch_related("Profession").all().filter(NumeroIdentification=Profession.Employe) 

    context = { 
     "Contribuable" : Contribuable, 
     } 

    return render(request, 'Fiscal_Reporting.html', context) 

在我的模板:

<table style="width:120%"> 
      <tbody> 
      <tr> 
       <th>ID</th> 
       <th>État</th> 
       <th>N° Identification</th> 
       <th>Civilité</th> 
       <th>Nom</th> 
       <th>Prénom</th> 
       <th>Date de Naissance</th> 
       <th>N° Fiscal</th> 
       <th>Salaire Annuel Net</th> 
      </tr> 
      {% for item in Contribuable %} 
      <tr> 
       <td>{{ item.id}}</td> 
       <td>{{ item.Etat}}</td> 
       <td>{{ item.NumeroIdentification}}</td> 
       <td>{{ item.Civilite }}</td> 
       <td>{{ item.Nom }}</td> 
       <td>{{ item.Prenom }}</td> 
       <td>{{ item.DateNaissance }}</td> 
       <td>{{ item.NumeroImpot }}</td> 
       <td>{{ item.Employe.Salaire_N_annuel }}</td> 
      </tr> 
      {% endfor %} 
      </tbody> 
     </table> 

关于我的问题,你知道吗?到目前为止我没有找到任何结果,因为我的数组是空的:/

然后,我如何在我的Django QuerySet中添加补充连接?

回答

1

您可以使用prefetch_related预取一个以上的相关表是这样的:

Contribuable = Individu.objects.prefetch_related("Individu", "Salaire_Individu") 

请注意,我用"Individu" related_name Profession模型prefetch_related说法。

现在的模板,你可以在每个迭代的职业和Salaire_Individu这样的:

{% for item in Contribuable %} 
    ... 
    <td>{{ item.Nom }}</td> 
    {% for profession in item.Individu.all %} 
     <td>{{ profession.NumeroImpot }}</td> 
    {% endfor %} 
    {% for salary in item.Salaire_Individu.all %} 
     <td>{{ salary.Salaire_B_mensuel }}</td> 
    {% endfor %} 
{% endfor %} 
+0

'select_related'更接近SQL加入该'prefetch_related',其实'select_related'是一个SQL JOIN。 https://stackoverflow.com/questions/31237042/whats-the-difference-between-select-related-and-prefetch-related-in-django-orm – luxcem

+0

我试过你的语法,但我在我的数组中遇到问题。我在'Contribuable'中循环每个项目,并且可以显示'Nom',...(来自'Individu'表格的字段)。但是我也应该显示Table'Profession'和'Salaire'的元素吧? 正如你可以看到我的问题:'item.Nom'工作正常,但'item.NumeroImpot'不显示元素。我的语法是错误的? – Deadpool

+0

@Deadpool尝试更新的变体。 – neverwalkaloner