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中添加补充连接?
'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
我试过你的语法,但我在我的数组中遇到问题。我在'Contribuable'中循环每个项目,并且可以显示'Nom',...(来自'Individu'表格的字段)。但是我也应该显示Table'Profession'和'Salaire'的元素吧? 正如你可以看到我的问题:'item.Nom'工作正常,但'item.NumeroImpot'不显示元素。我的语法是错误的? – Deadpool
@Deadpool尝试更新的变体。 – neverwalkaloner