Reputation: 30
I started Django since 2 weeks and I try to obtain information of the table 'Salary' with regard to the pk ' matriculeemp ' selected in the url,'matriculeemp' is the foreign key of 'Salary' and the pk of the table employe.
I was not able to to find the answer by looking on other post of stackoverflow nor by posting mine on other forums, thus I come to you. I have these models and this view. These paths in my urls.py of my folder of application :
Models
class Salaire(models.Model):
date = models.DateField(db_column='Date')
charge_patronale_mensuel = models.FloatField(db_column='Charge_Patronale_Mensuel', blank=True, null=True)
charge_salariale_mensuel= models.FloatField(db_column='Charge_Salariale_Mensuel', blank=True, null=True)
autre_charge_mensuel_fixe = models.FloatField(db_column='Autre_Charge_Mensuel_Fixe', blank=True, null=True)
salaire_brut_mensuel = models.FloatField(db_column='Salaire_Brut_Mensuel', blank=True, null=True)
matriculeemp = models.ForeignKey(Employe, models.DO_NOTHING, db_column='MatriculeEmp', blank=True, null=True)
salaire_net_mensuel = models.FloatField(db_column='Salaire_Net_Mensuel', blank=True, null=True)
charge_variable_mensuel = models.FloatField(db_column='Charge_Variable_Mensuel', blank=True, null=True)
class Meta:
db_table = 'salaire'
ordering = ["date"]
class Employe(models.Model):
matriculeemp = models.AutoField(db_column='MatriculeEmp', primary_key=True)
nomemp = models.CharField(db_column='NomEmp', max_length=15, blank=True, null=True)
prenomemp = models.CharField(db_column='PrenomEmp', max_length=15, blank=True, null=True)
datenaissanceemp = models.DateField(db_column='DateNaissanceEmp', blank=True, null=True)
dateembaucheemp = models.DateField(db_column='DateEmbaucheEmp', blank=True, null=True)
genreemp = models.CharField(db_column='GenreEmp', max_length=10, blank=True, null=True)
codefonc = models.ForeignKey('Fonction', models.DO_NOTHING, db_column='CodeFonc', blank=True, null=True) # Field name made lowercase.
codecateg = models.ForeignKey(Categorie, models.DO_NOTHING, db_column='CodeCateg', blank=True, null=True) # Field name made lowercase.
codeville = models.ForeignKey('VilleEmp', models.DO_NOTHING, db_column='CodeVille', blank=True, null=True) # Field name made lowercase.
codedepartement = models.ForeignKey(DepartementEmp, models.DO_NOTHING, db_column='CodeDepartement', blank=True, null=True) # Field name made lowercase.
date_de_sortie = models.DateField(db_column='Date_De_Sortie', blank=True, null=True)
def __str__(self):
"""
String for representing the Model object (in Admin site etc.)
"""
return self.nomemp
class Meta:
db_table = 'employe'
ordering = ["nomemp"]
verbose_name = "Employé"
Views
class EmployeDetailView(DetailView):
model = Employe
def employeDetailView(self,request,pk,salaire__matriculeemp):
try:
employe=Employe.objects.get(pk=pk)
salaire=get_object_or_404(pk=matriculeemp__matriculeemp)
#Book.objects.filter(publisher__name='BaloneyPress').count()
#albumgroupe= Album.objects.filter(nomgroupe__nomgroupe__contains=request.POST['searchgroupe'])
except Employe.DoesNotExist:
raise ("Book does not exist")
return render(
request,
'Compta-IMC/employe_detail.html',
context={'employe':employe, 'salaire':salaire}
)
urls
urlpatterns = [
path('', views.index, name='index'),
path('employes/', views.EmployeListView.as_view(), name='employes'),
#path('employe/<int:pk>', views.EmployeDetailView.as_view(), name='employe-detail'),
path('employe/<int:pk>', EmployeDetailView.as_view(), name='employe-detail'),
]
My goal is to retrieve all the information of a salary in relation to the matriculeemp pass in pk in the url, to post them in my template employee_detail.html, thank you in advance for your help.
Upvotes: 0
Views: 113
Reputation: 3051
I don't know why you are defining different method employeDetailView
in your DetailView
.
You can simply define template
and use context_object_name
in the html as follows.
employe.salaire_set
gives all related Salary data for that employee.
class EmployeDetailView(DetailView):
model = Employe
template_name = "Compta-IMC/employe_detail.html"
context_object_name = 'employe'
{% for salarie in employe.salaire_set.all %}
{{salarie.date}}
....
{% endfor %}
For getting latest item, you can define Meta
property in your Model
as follows:
class Salaire(models.Model): .... ....
class Meta:
get_latest_by = 'date'
Now, you can get the latest item as :
{{employe.salaire_set.latest.date}}
{{employe.salaire_set.latest.charge_patronale_mensuel}}
Upvotes: 1
Reputation: 645
From what i understood from the question you want to fetch the Salarie based on the employee's id Then this should work fine to load data from Salarie table
class EmployeDetailView(DetailView):
model = Employe
def employeDetailView(self,request,pk,salaire__matriculeemp):
try:
employe=Employe.objects.get(pk=pk)
salaire=get_object_or_404(matriculeemp=employe)
#Book.objects.filter(publisher__name='BaloneyPress').count()
#albumgroupe= Album.objects.filter(nomgroupe__nomgroupe__contains=request.POST['searchgroupe'])
except Employe.DoesNotExist:
raise ("Book does not exist")
return render(
request,
'Compta-IMC/employe_detail.html',
context={'employe':employe, 'salaire':salaire}
)
Let me know if this works
Upvotes: 0