0

In The Models.py file (I have this codebase)

class Person(models.Model):
    sex_choices = (
        ('Male', 'Male'),
        ('Female', 'Female')
    )
    martial_choices = (
        ('Single', 'Single'),
        ('Married', 'Married'),
        ('Divorce', 'Divorce'),
        ('Widowed', 'Widowed')
    )

    name = models.CharField(max_length=200)
    sex = models.CharField(choices=sex_choices, max_length=50)
    martial_status = models.CharField(choices=martial_choices, max_length=50)
    age = models.IntegerField()


    def __str__(self):
        return self.name


class DetailsOfEducationQualification(models.Model):
    type_choice = (
        ("Government", "Government"),
        ("Private", "Private"),
        ("Anganwadi Center", "Anganwadi Center"),
    )
    education_proximity_choice = (
        ("0-5", '0-5km'),
        ('5-10', '5-10km'),
        ('10+', 'Above 10km'),
        ('Outside the state', 'Outside the state'),
    )

    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    course_class = models.CharField(max_length=50, blank=True)
    type_of_education_sector = models.CharField(choices=type_choice, max_length=50, blank=True)
    education_facility_proximity = models.CharField(choices=education_proximity_choice, max_length=50, blank=True)

In The Admin.py file (I have this)

from .models import (
    Person, DetailsOfEducationQualification
)

class DetailsOfEducationQualificationInline(admin.TabularInline):
    model = DetailsOfEducationQualification
    extra = 0

    class PersonAdmin(admin.ModelAdmin):
        fieldsets = [
            (
                'Personal Information', {
                'fields':[
                    'name', 'sex', 'age', 'martial_status'
                ]
            }
        ),
    ]
    inlines = [
        DetailsOfEducationQualificationInline
    ]

In query shell, I want to get the person 'course_class' since the DetailsOfEducationQualification model is related to Person:

like in he query:

person = Person.objects.get(id=1)
person.course_class

this code gets an error, saying person does not have the attribute... How do I access the DetailsOfEducationQualification date from the Person model?

Tigran
  • 632
  • 1
  • 5
  • 21
wan0004
  • 1
  • 1

1 Answers1

0

course_class is an attribute of DetailsOfEducationQualification class, and not an attribute of Person class,

so you won't be able to access it from a person object.

detail = DetailsOfEducationQualification.objects.get(id=1)
detail.course_class

the above code will return the course_class of DetailsOfEducationQualification with id 1.

if you want to access course_class from a Person object,

then you can use

person = Person.objects.get(id=1)
person.detailsofeducationqualification_set.first().course_class

person.detailsofeducationqualification_set is the Queryset of all DetailsOfEducationQualification objects that has person with id 1 as foreign key.

and, person.detailsofeducationqualification_set.first() will be the first element of the queryset.

hope it helps.

  • Can you elaborate little more, I can't really take in much from this ** person.detailsofeducationqualification_set.first().course_class**... is this to be used in the pyhton-django shell or in the models.py file? Thank You!!! – wan0004 May 08 '20 at 15:18