0

I am trying to generate a query for which I get the expected result in django shell, but for the same query, I am getting an error that the attribute for the model does not exist.

First the shell:

>>> from dbaccess.models import *
>>> applicantObject = Applicant.objects.get(pk=5)
>>> vol = VolInterview.objects.get(applicant=applicantObject)
>>> vol
<VolInterview: Rajon>

From views.py

from models import *

def addIntCandidate(request):
    applicants = Applicant.objects.filter(applicationStatus="Pending")
    interviews = Interview.objects.all()
    message = []
    if request.method == 'POST':
        applicant = request.POST.get('applicant')
        ...
        # the value of applicant at this point is 5
        applicantObject = Applicant.objects.get(pk=applicant)
        prevRejected = VolInterview.objects.get(applicant=applicantObject)
        ...

Error message:

type object 'VolInterview' has no attribute 'objects'

Traceback:

E:\projects_directory\djangoprojects\kpr-admin-db\dbaccess\views.py in addIntCandidate
                    prevRejected = VolInterview.objects.get(applicant=applicantObject)

What am I doing wrong?

Roy
  • 59
  • 6
  • Could you have replaced `VolInterview` with a different class in your views.py? To check, add `print(VolInterview)` to your view and the shell, and check that you get the same result. – Alasdair Jan 08 '16 at 11:46
  • I actually imported another form with the same name. Thanks for pointing out. Could you post in the answer section, so that I could accept it? – Roy Jan 08 '16 at 11:51
  • @Alasdair, could me guide me to how I could combine both the queries into a single one? -> applicantObject = Applicant.objects.get(pk=applicant) and prevRejected = VolInterview.objects.get(applicant=applicantObject) – Roy Jan 08 '16 at 11:53
  • 3
    **NEVER** use star imports (`from module import *`) in your code - this "feature" should only be used in a Python shell. – bruno desthuilliers Jan 08 '16 at 11:54
  • 2
    @roy: `prevRejected = VolInterview.objects.get(applicant__pk=applicant)` (cf https://docs.djangoproject.com/en/1.9/topics/db/queries/#lookups-that-span-relationships), though in this case you can just use `VolInterview.objects.get(applicant=applicant)`. But please post new questions as new questions, not as (totally unrelated) comment on this question. – bruno desthuilliers Jan 08 '16 at 11:58
  • `.get(applicant_id=applicant)` might be even better. – sobolevn Jan 08 '16 at 12:03
  • @brunodesthuilliers, thanks for tip. I just thought since I was going to use all the class definitions, I went for the *. But now I can see how that could go wrong. – Roy Jan 08 '16 at 12:04

1 Answers1

1

It looks like you have replaced VolInterview with a different class in your views.

To check, you can add print(VolInterview) to your view and the shell, and check that you get the same result in both.

In Django, it is common to use Form as a suffix for your form classes, e.g. VolInterviewForm, so that the model and form names do not clash.

Alasdair
  • 298,606
  • 55
  • 578
  • 516