Reputation: 20222
I defined a Question model with a description and a foreign key.
class Question(models.Model):
user = models.ForeignKey(
User,
verbose_name="User",
default=None
)
description = models.CharField(
max_length=60,
#verbose_name=_("Description"),
)
After that, I ran the migrations.
Then, in views.py
I created a method which accesses objects of this model:
def own_questions(request):
questions = Question.objects()
return JsonResponse(questions)
The problem is that when I access the URL /questions
corresponding to this method, I get:
NameError at /questions/
global name 'Question' is not defined
Why is this happening?
Upvotes: 0
Views: 741
Reputation: 25539
You need to import Questions
in your views.py:
from app.models import Question
Also, questions = Question.objects
only give you the queryset manager and you can't call that, instead for all questions, you need:
questions = Question.objects.all()
Edit:
I shouldn't assume what you are trying to query from model Question
, so here's django doc about how to write ORM for queries.
Upvotes: 2
Reputation: 3232
Probably because you haven't imported
from .models import Question
into your views.py
Upvotes: 3