pythondjangodjango-modelsdjango-related-manager

Related Field got invalid lookup while making a model relationship


I am trying to make relationships traversing through different models but am getting this error :


django.core.exceptions.FieldError: Related Field got invalid lookup: occupational_group

below are the models I have :

class LoanApplication(models.Model, TimeStampedModel):
    loan_taker = models.ForeignKey(
        CustomerProfile, on_delete=models.PROTECT, null=True, blank=True,
        verbose_name=_('Customer Profile'), related_name='loan_entries')


class CustomerProfile(models.Model, TimeStampedModel):

    is_copy_component = models.BooleanField(
        _('Is copy component'), default=False)
    cooperation_partner = models.ForeignKey(
        EmailUser, on_delete=models.PROTECT, null=True, blank=True,
        verbose_name=_('Jurisdiction'), related_name='partner_user_profile')
    user = models.OneToOneField(
        EmailUser, on_delete=models.PROTECT, null=True, blank=True,
        verbose_name=_('User'), related_name='user_profile')
    approval_inquiry_sent_at = models.DateTimeField(
        _('Approval inquiry sent at'), null=True, blank=True)
    email_content_customer = models.FileField(
        _('Content of the customer email'), upload_to="customer-emails/",
        blank=True)
    approved_at = models.DateTimeField(
        _('Approved at'), null=True, blank=True)



class CustomerProfilePerson(models.Model, TimeStampedModel):


    person = models.ForeignKey(
        Person, on_delete=models.CASCADE,
        verbose_name=_('Person'), related_name='customer_profile_relation')
    customer_profile = models.ForeignKey(
        CustomerProfile, on_delete=models.CASCADE,
        verbose_name=_('Customer Profile'), related_name='persons')


class Person(models.Model, TimeStampedModel):

    occupational_group = models.CharField(
        _('Occupational group'), max_length=16, blank=True,
        choices=OCCUPATIONAL_GROUP_CHOICES)

Am trying to filter the LoanApplication model with the relationship of an occupation group, below is the list of the occupational group :

OCCUPATIONAL_GROUP_CHOICES = (
    ('pharmacist', _('Pharmacist')),
    ('architect', _('Architect')),
    ('doctor', _('Doctor')),
    ('consulting_eng', _('Consulting engineer')),
    ('notary', _('Notary')),
    ('psychotherapist', _('Psychotherapist')),
    ('lawyer', _('Lawyer')),
    ('tax_consultant', _('Tax Consultant')),
    ('vet', _('Vet')),
    ('sworn_auditor', _('Sworn auditor')),
    ('surveyor', _('Surveyor')),
    ('auditor', _('Auditor')),
    ('dentist', _('Dentist')),
    ('other', _('Other')),
)

So this is my query below but failing :

LoanApplication.objects.filter(loan_taker__persons__person__occupational_group__in: ['pharmacist'])

Am getting this exception :

django.core.exceptions.FieldError: Related Field got invalid lookup: occupational_group

Solution

  • You pass a parameter to the function with a colon (:), not an equal sign (=), you thus should filter with:

    LoanApplication.objects.filter(
        loan_taker__persons__person__occupational_group__in=['pharmacist']
    )

    since you only match with a list with one item, you can replace this with:

    LoanApplication.objects.filter(
        loan_taker__persons__person__occupational_group='pharmacist'
    )