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?

7
  • 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. Commented Jan 8, 2016 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? Commented Jan 8, 2016 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) Commented Jan 8, 2016 at 11:53
  • 3
    NEVER use star imports (from module import *) in your code - this "feature" should only be used in a Python shell. Commented Jan 8, 2016 at 11:54
  • 2
    @roy: prevRejected = VolInterview.objects.get(applicant__pk=applicant) (cf docs.djangoproject.com/en/1.9/topics/db/queries/…), 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. Commented Jan 8, 2016 at 11:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.