'Patients' object has no attribute 'total_labo_set'

Hello Django family
in fact, I want to recover the price of consultations per person here

def detail(request,pk):
    
    patient=Patients.objects.get(Idpatient=pk)
total_labo=patient.total_labo_set.filter(date_creation_labo=date.today()).aggregate(Sum('somme_labo'))['somme_labo__sum']
    total_specialiste=patient.total_specialiste_set.filter(date_creation_pecialiste=date.today()).aggregate(Sum('somme_specialiste'))['somme_specialiste__sum']
    total_generaliste=patient.total_generaliste_set.filter(date_creation_generaliste=date.today()).aggregate(Sum('somme_generaliste'))['somme_generaliste__sum']
    context={'patient':patient,'labos':labos,'specialistes':specialistes,'generalistes':generalistes,'ladate':ladate,'total_labo':total_labo,'total_specialiste':total_specialiste,'total_generaliste':total_generaliste}
    return render(request,'recu/detail.html',context)

and here is the error it shows me
‘Patients’ object has no attribute ‘total_labo_set’
I ask for help to be on the right track thank you.

We’ll need to see your Patients model along with the other model being referenced.

Also, this more appropriately belongs in the Using Django category, not Django Internals. You can recategorize it by clicking on the pencil icon next to the title and selecting the proper category.

1 Like

I am delighted, thank you and here is my model

class Patients(models.Model):
    Idpatient=models.AutoField(primary_key=True)
    date_creation=models.DateField(auto_now_add=True,null=True)
    nom_prenom=models.CharField(max_length=200,null=True)
    date_naissance=models.DateField(max_length=100,null=True)
    contact=models.CharField(max_length=30,null=True)
    adresse=models.CharField(max_length=100,null=True)
    assurance=models.CharField(max_length=100,null=True,default='non défini')
    compagnie=models.CharField(max_length=100,null=True,default='non défini')
    matricule=models.CharField(max_length=100,null=True,default='non défini')
    medecin=models.CharField(max_length=100,null=True)
    def __str__(self):
        return self.nom_prenom

# class patientAdmin
class patientAdmin(admin.ModelAdmin):
    list_display = ('date_creation','nom_prenom','date_naissance','contact','adresse','assurance','compagnie','medecin')
    list_filter = ('nom_prenom','contact')
    search_fields = ('nom_prenom','contact')
    


class Labos(models.Model):
    EXAMEN=(
        ("Numération Formule Sanguine (NFS) B 30","Numération Formule Sanguine (NFS) B 30"),
        ("Numération des réticulocytes B 25","Numération des réticulocytes B 25"),
        ("Vitesse de sédimentation B 10","Vitesse de sédimentation B 10"),
        ("Electrophorèse de l'hémoglobine à pH alcalin B 60","Electrophorèse de l'hémoglobine à pH alcalin B 60"),
        ("Antithrombine III (AT III) B 40", "Antithrombine III (AT III) B 40"),
        ("Taux de prothrombine (TP) B 20", "Taux de prothrombine (TP) B 20"),
        ("Temps de céphaline avec activateur (TCA ou TCK) B 20","Temps de céphaline avec activateur (TCA ou TCK) B 20"),
        ("Fibrinémie ou fibrinogène B 20","Fibrinémie ou fibrinogène B 20"),
        ("Examen parasitologique des selles (EPS ou KOP) B 25","Examen parasitologique des selles (EPS ou KOP) B 25"),
        ("Goutte épaisse (GE) B 25","Goutte épaisse (GE) B 25"),
        ("Frottis sanguin (Recherche d'hématozoaires B 25)","Frottis sanguin (Recherche d'hématozoaires B 25)"),
        ("PSA total B 150","PSA total B 150"),
        ("PSA libre B 150","PSA libre B 150"),
        ("CA 15-3 B 150","CA 15-3 B 150"),
        ("CA 19-9 B 150","CA 19-9 B 150"),
        ("CA 125 B 150","CA 125 B 150"),
        ("Antigène Carcino-embryonnaire (ACE) B 150","Antigène Carcino-embryonnaire (ACE) B 150"),
        ("Alpha-foetoprotéine (AFP) B 150","Alpha-foetoprotéine (AFP) B 150"),
        ("charge virale VIH B 300","charge virale VIH B 300"),
        ("charge virale Hépatite B 300","charge virale Hépatite B 300"),
        ("Charge virale Hépatite C (RNA-HCV) B 300","Charge virale Hépatite C (RNA-HCV) B 300"),
        ("Acide urique (Uricémie) B 10","Acide urique (Uricémie) B 10"),
        ("Amylase B 30","Amylase B 30"),
        ("Bilirubine directe ou bilirubine conjuguée B 25","Bilirubine directe ou bilirubine conjuguée B 25"),
        ("Bilirubine totale B 25","Bilirubine totale B 25"),
        ("Calcium plasmatique (Ca) B 20","Calcium plasmatique (Ca) B 20"),
        ("Cholestérol total B 10","Cholestérol total B 10"),
        ("Cholestérol HDL B 35","Cholestérol HDL B 35"),
        ("Cholestérol LDL B 35","Cholestérol LDL B 35"),
        ("CK-MB B 20","CK-MB B 20"),
        ("CPK TOTAL (Créatine phosphokinase) B 20","CPK TOTAL (Créatine phosphokinase) B 20"),
        ("Créatinine (Créatininémie) B 10","Créatinine (Créatininémie) B 10"),
        ("Electrophorèse des protéines (Protidogramme) B 60","Electrophorèse des protéines (Protidogramme) B 60"),
        ("Fer sérique B 30","Fer sérique B 30"),
        ("Ferritine (Ferritinémie) B 150","Ferritine (Ferritinémie) B 150"),
        ("Gamma- GT (γ-GT) B 20","Gamma- GT (γ-GT) B 20"),
        ("Glycémie à jeun B 10","Glycémie à jeun B 10"),
        ("Hémoglobine glycosylée (HbA1C) B 70","Hémoglobine glycosylée (HbA1C) B 70"),
        ("Hyperglycémie provoquée par voie orale (HGPO) B 60","Hyperglycémie provoquée par voie orale (HGPO) B 60"),
        ("Ionogramme sanguin (Na+,K+,Cl-) B 35","Ionogramme sanguin (Na+,K+,Cl-) B 35"),
        ("LDH (Lactate déshydrogénase) B 30","LDH (Lactate déshydrogénase) B 30"),
        ("Lipase B 30","Lipase B 30"),
        ("Lipides totaux B 25","Lipides totaux B 25"),
        ("Magnésium plasmatique (Mg) B 20","Magnésium plasmatique (Mg) B 20"),
        ("Myoglobine B 40","Myoglobine B 40"),
        ("Phosphatases alcalines (PAL) B 25","Phosphatases alcalines (PAL) B 25"),
        ("Phosphore (ou Phosphates) B 15","Phosphore (ou Phosphates) B 15"),
        ("Protéines totales (Protides totaux) B 15","Protéines totales (Protides totaux) B 15"),
        ("Test de O'Sullivan B 70","Test de O'Sullivan B 70"),
        ("Transaminases (ASAT ou GOT+ALAT ou GPT) B 40","Transaminases (ASAT ou GOT+ALAT ou GPT) B 40"),
        ("Triglycérides B 10","Triglycérides B 10"),
        ("Urée B 10","Urée B 10"),
        ("Albumine B 07","Albumine B 07"),
        ("Sucre B 05","Sucre B 05"),
        ("Acétone B 05","Acétone B 05"),
        ("Inogramme urinaire B 35","Inogramme urinaire B 35"),
        ("Protéinurie des 24 heures B 15","Protéinurie des 24 heures B 15"),
        ("Microalbuminurie B 100","Microalbuminurie B 100"),
        ("ACTH B 150","ACTH B 150"),
        ("FSH (Hormone folliculo-stimulante) B 150","FSH (Hormone folliculo-stimulante) B 150"),
        ("LH (Hormone lutéinisante) B 150","LH (Hormone lutéinisante) B 150"),
        ("Oestradiol B 150","Oestradiol B 150"),
        ("Progestérone B 150","Progestérone B 150"),
        ("Prolactine B 150","Prolactine B 150"),
        ("β HCG Plamatique B 150","β HCG Plamatique B 150"),
        ("Cortisolémie B 150","Cortisolémie B 150"),
        ("Testostérone B 150","Testostérone B 150"),
        ("TSH ultrasensible B 150","TSH ultrasensible B 150"),
        ("T3 libre (FT3) B 150","T3 libre (FT3) B 150"),
        ("T4 libre (FT4) B 150","T4 libre (FT4) B 150"),
        ("T3 B 150","T3 B 150"),
        ("T4 B 150","T4 B 150"),
        ("Antibiogramme (ATB) B 65","Antibiogramme (ATB) B 65"),
        ("Antifongigramme (ATF) B 65","Antifongigramme (ATF) B 65"),
        ("Pus:Cytobactériologie + Antibiogramme B 90","Pus:Cytobactériologie + Antibiogramme B 90"),
        ("LCR: Chimie B 40","LCR: Chimie B 40"),
        ("LCR: Cytobactériologie + Antibiogramme B 90","LCR: Cytobactériologie + Antibiogramme B 90"),
        ("LCR: Cytobactériologie + ATB - Chimie B 130","LCR: Cytobactériologie + ATB - Chimie B 130"),
        ("Liquide ascite: Chimie - Cytobactériologie + ATB B 130","Liquide ascite: Chimie - Cytobactériologie + ATB B 130"),
        ("Liquide pleural:Cytobactériologie + ATB B 90","Liquide pleural:Cytobactériologie + ATB B 90"),
        ("Liquide pleural: Chimie B 40","Liquide pleural: Chimie B 40"),
        ("Liquide pleural: Chimie-Cytobactériologie + ATB B 130","Liquide pleural: Chimie-Cytobactériologie + ATB B 130"),
        ("Liquide pleural:Cytobactériologie + ATB - Recherche de BK B 150","Liquide pleural:Cytobactériologie + ATB - Recherche de BK B 150"),
        ("PV (Examen standard-Culture- Mycoplasmes- Chlamydia - ATB- ATF) B 200","PV (Examen standard-Culture- Mycoplasmes- Chlamydia - ATB- ATF) B 200"),
        ("PU (Examen standard-Culture- Mycoplasmes- Chlamydia - ATB) B 180","PU (Examen standard-Culture- Mycoplasmes- Chlamydia - ATB) B 180"),
        ("ECBU B 65","ECBU B 65"),
        ("Coproculture B 65","Coproculture B 65"),
        ("Hémoculture B 65","Hémoculture B 65"),
        ("ECBU + Antibiogramme/Antifongigramme B 130","ECBU + Antibiogramme/Antifongigramme B 130"),
        ("Coproculture + Antibiogramme/Antifongigramme B 130","Coproculture + Antibiogramme/Antifongigramme B 130"),
        ("Hemoculture + Antibiogramme B 130","Hemoculture + Antibiogramme B 130"),
        ("Spermogramme + Spermocytogramme B 100","Spermogramme + Spermocytogramme B 100"),
        ("Spermoculture + Antibiogramme B 130","Spermoculture + Antibiogramme B 130"),
        ("Ac anti- HAV B 70 ","Ac Anti- HAV B 70 "),
        ("Ac anti- HAV IgM B 70","Ac anti- HAV IgM B 70"),
        ("Ac anti- HBc totaux B 70","Ac anti- HBc totaux B 70"),
        ("Ac anti- HBc IgM B 70","Ac anti- HBc IgM B 70"),
        ("Ac anti- HBe B 70","Ac anti- HBe B 70"),
        ("Ac anti- HBs (Test qualitatif) B 70","Ac anti- HBs (Test qualitatif) B 70"),
        ("Ac anti- HBs (Tirage) B 100","Ac anti- HBs (Tirage) B 100"),
        ("Ac anti- HVC (Hépatite C) B 70","Ac anti- HVC (Hépatite C) B 70"),
        ("Ag HBe B 70","Ag HBe B 70"),
        ("Ag HBs B 70","Ag HBs B 70"),
        ("Antistreptolysine O (ASLO) B 35","Antistreptolysine O (ASLO) B 35"),
        ("Sérodiagnostic de Widal et Félix B 40","Sérodiagnostic de Widal et Félix B 40"),
        ("Sérologie amibienne B 60","Sérologie amibienne B 60"),
        ("Sérologie bilhazienne B 60","Sérologie bilhazienne B 60"),
        ("Sérologie palustre B 60","Sérologie palustre B 60"),
        ("Sérologie de la syphilis (TPHA- VDRL) B 40","Sérologie de la syphilis (TPHA- VDRL) B 40"),
        ("Sérologie des Chlamydia B 160","Sérologie des Chlamydia B 160"),
        ("Sérologie des mycoplasmes B 100","Sérologie des mycoplasmes B 100"),
        ("Sérologie de la rubéole (IgG - IgM) B 120","Sérologie de la rubéole (IgG - IgM) B 120"),
        ("Sérologie de la toxoplasmose (IgG - IgM) B 80","Sérologie de la toxoplasmose (IgG - IgM) B 80"),
        ("Sérologie Cytomégalovirus (CMV) B 160","Sérologie Cytomégalovirus (CMV) B 160"),
        ("Sérologie herpétique (HSV 1 et HSV 2) B 160","Sérologie herpétique (HSV 1 et HSV 2) B 160"),
        ("IgE totales B 150","IgE totales B 150"),
        ("Facteurs rhumatoïdes B 50","Facteurs rhumatoïdes B 50"),
    )
    PRIX=(
        (0,0),
        (100,100),
        (200,200),
        (250,250),
        (280,280),
        (300,300),
        (350,350),
        (400,400),
        (450,450),
        (2500,2500),
    )
    
    POURCENTAGE=(
        (0,0),
        (1,1),
        (0.1,0.1),
        (0.15,0.15),
        (0.2,0.2),
        (0.3,0.3),
        (0.4,0.4),
        (0.5,0.5),
    )
    Idlabo=models.AutoField(primary_key=True)
    date_creation_labo=models.DateField(auto_now_add=True)
    examen=models.CharField(max_length=100,null=True,choices=EXAMEN)
    cotation=models.IntegerField(null=True,default=1)
    prix=models.IntegerField(null=True,choices=PRIX)
    pourcentage=models.FloatField(null=True,choices=POURCENTAGE)
    somme_labo=models.FloatField(null=True,editable=False)
    patient=models.ForeignKey(Patients,null=True,on_delete=models.SET_NULL)
    def __str__(self):
        return self.examen

    @property
    def get_somme_labo(self):
        if(self.pourcentage==0):
            sommation=float(self.cotation*self.prix)
            return sommation
        elif (self.pourcentage==1):
            sommation=0
            return sommation
        else:
            sommation=float((self.cotation*self.prix)*self.pourcentage)
            return sommation

    def save(self, *args, **kwargs):
        self.somme_labo =self.get_somme_labo
        super(Labos,self).save(*args, **kwargs)
    
    def __float__(self):
        self.somme_labo


# class laboadmin
class laboAdmin(admin.ModelAdmin):
    list_display = ('date_creation_labo','examen','cotation','prix','pourcentage','somme_labo','patient')
    list_filter = ('date_creation_labo','patient')
    search_fields = ('date_creation_labo','patient')

    
class Specialistes(models.Model):

    POURCENTAGE=(
        (0,0),
        (1,1),
        (0.1,0.1),
        (0.15,0.15),
        (0.2,0.2),
        (0.3,0.3),
        (0.4,0.4),
        (0.5,0.5),
    )
    Idspecialiste=models.AutoField(primary_key=True)
    date_creation_pecialiste=models.DateField(auto_now_add=True,null=True)
    nom_examen_specialiste=models.CharField(max_length=100,null=True)
    prix_specialiste=models.FloatField(null=True)
    somme_specialiste=models.FloatField(null=True,editable=False)
    pourcentage=models.FloatField(null=True,choices=POURCENTAGE)
    patient=models.ForeignKey(Patients,null=True,on_delete=models.SET_NULL)
    def __str__(self):
        return self.nom_examen_specialiste

    @property
    def get_somme_specialiste(self):
        if(self.pourcentage ==0):
            sommation_spec=float(self.prix_specialiste)
            return sommation_spec
        elif (self.pourcentage==1):
            sommation_spec=0
            return sommation_spec
        else:
            sommation_spec=float(self.prix_specialiste*self.pourcentage)
            return sommation_spec

    def save(self, *args, **kwargs):
        self.somme_specialiste =self.get_somme_specialiste
        super(Specialistes,self).save(*args, **kwargs)
    
    def __float__(self):
        self.somme_specialiste
        
# class specialisteadmin

class specialisteAdmin(admin.ModelAdmin):
    list_display = ('date_creation_pecialiste','nom_examen_specialiste','prix_specialiste','pourcentage','somme_specialiste','patient')
    list_filter = ('date_creation_pecialiste','patient')
    search_fields = ('date_creation_pecialiste','patient')



class Generalistes(models.Model):
    POURCENTAGE=(
        (0,0),
        (1,1),
        (0.1,0.1),
        (0.15,0.15),
        (0.2,0.2),
        (0.3,0.3),
        (0.4,0.4),
        (0.5,0.5),
    )
    Idgeneraliste=models.AutoField(primary_key=True)
    date_creation_generaliste=models.DateField(auto_now_add=True,null=True)
    nom_examen_generaliste=models.CharField(max_length=100,null=True)
    prix_generaliste=models.FloatField(null=True)
    somme_generaliste= models.FloatField(null=True,editable=False)
    pourcentage=models.FloatField(null=True,choices=POURCENTAGE)
    patient=models.ForeignKey(Patients,null=True,on_delete=models.SET_NULL)
    def __str__(self):
        return self.nom_examen_generaliste
    @property
    def get_somme_generaliste(self):
        if(self.pourcentage ==0):
            sommation_gen=float(self.prix_generaliste)
            return sommation_gen
        elif (self.pourcentage==1):
            sommation_gen=0
            return sommation_gen
        else:
            sommation_gen=float(self.prix_generaliste*self.pourcentage)
            return sommation_gen

    def save(self, *args, **kwargs):
        self.somme_generaliste =self.get_somme_generaliste
        super(Generalistes,self).save(*args, **kwargs)
    
    def __float__(self):
        self.somme_generaliste
            
# class generalisteAdmin
class generalisteAdmin(admin.ModelAdmin):
    list_display=('date_creation_generaliste','nom_examen_generaliste','prix_generaliste','pourcentage','somme_generaliste','patient')
    list_filter=('date_creation_generaliste','nom_examen_generaliste')
    search_fields =('date_creation_generaliste','nom_examen_generaliste')

and I apologize for the length of my model

I don’t see a table named Total_Labo, just one named Labos with a ForeignKey back to Patients. So the reverse FK reference would be labos_set.

1 Like

great thank you very much I understand

but if I call it several times in a view will it not have a problem with that?

def detail(request,pk):
    
    patient=Patients.objects.get(Idpatient=pk)
labos=patient.labos_set.filter(date_creation_labo=date.today())
    specialistes=patient.specialistes_set.filter(date_creation_pecialiste=date.today())
    generalistes=patient.generalistes_set.filter(date_creation_generaliste=date.today())
labos=patient.labos_set.filter(date_creation_labo=date.today()).aggregate(Sum('somme_labo'))['somme_labo__sum']
    specialistes=patient.specialistes_set.filter(date_creation_pecialiste=date.today()).aggregate(Sum('somme_specialiste'))['somme_specialiste__sum']
    generalistes=patient.generalistes_set.filter(date_creation_generaliste=date.today()).aggregate(Sum('somme_generaliste'))['somme_generaliste__sum']
    context={'patient':patient,'labos':labos,'specialistes':specialistes,'generalistes':generalistes,'ladate':ladate,'labos':labos,'specialistes':specialistes,'generalistes':generalistes}
    return render(request,'recu/detail.html',context)

Don’t confuse these two separate items

labos=patient.labos_set.filter(date_creation_labo=date.today())
              ^^^^^^^^^ the reverse foreign key reference
^^^^^ Variable being used to hold the queryset
1 Like

Obviously, I tell myself if I write it as described so high it will have a problem

or can we change the variable to make a difference?

Yes, that’s the point. You want to save the different results to different variables.

1 Like

precisely, thank you; that’s what i want to do again thanks