Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 585031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:57:38+00:00 2026-05-13T14:57:38+00:00

In a django app, I have models for a Word (to be learned), a

  • 0

In a django app, I have models for a Word (to be learned), a Student (learning it), and StudentWord is a table to handle the many to many relationship:

class Word(models.Model):
    word = models.CharField(max_length=80)
    image = models.ForeignKey(Image)
    language = models.ForeignKey(Language)
    def __unicode__(self):
        return self.word

class Student(models.Model):
    username = models.ForeignKey(User)
    words = models.ManyToManyField(Word, through='StudentWord')
    def __unicode__(self):
        return self.username.username

class StudentWord(models.Model):
    word = models.ForeignKey(Word)
    student = models.ForeignKey(Student)
    level = models.IntegerField()
    nextdate = models.DateField()  <-- this field newly added
    learned = models.BooleanField()
    def __unicode__(self):
        return u'%s %s' % (self.student, self.word)

I had it working, but wanted to add a feature where the app would know when was the next date to ask the student this word. For this, I added the nextdate field to the StudentWord model, deleted the studentword table in MySQL, used syncdb to regenerate it, and used the admin page to successfully add a few studentwords (with the new field for date in it).

However, the following part of the view is generating an error:

def index(request):
    last_question = request.session.get('last_question', 'none')
    student_language = request.session.get('student_language', 'english')
    student=Student.objects.get(username=request.user)
    words_student_knows = Word.objects.filter(studentword__student=student, studentword__learned=True)
    words_student_knows.filter(studentword__nextdate<=datetime.date.today())

The error is:

Exception Type:   NameError
Exception Value:  
global name 'studentword__nextdate' is not defined
Exception Location:  /home/wordcode/words/vocabulary/views.py in index, line 32

Line 32 is the last line of the snippet from the view above. When I remove that filter, the error goes away, so it seems to be related to the new field. I used:
import datetime
…in the view, and I am able to use datetime.date.today() elsewhere in the code without a problem.

My guess was that somehow it hasn’t picked up on the newly added field, but since the admin site understands that it’s there, that doesn’t seem to likely, and in any event deleting the studentword table and recreating it ought to do the trick.

Any help or advice is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:57:38+00:00Added an answer on May 13, 2026 at 2:57 pm

    You’ve misunderstood how you pass comparisons in Django model filters. Instead of your last line, you need to do this:

    words_student_knows.filter(studentword__nextdate__lte=datetime.date.today())
    

    Spelled this way, the filter is correctly passed to the model manager as a keyword argument, which is then broken down into its constituent parts and processed successfully within the manager.

    The way you had it, though, meant that Python was trying to evaluate the result of studentword__nextdate<=datetime.date.today() right there in the calling function, aiming to pass a Boolean value to the destination function. Of course, there’s no local variable called studentword__nextdate, hence the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 models in my django app class Company(models.Model): name = models.CharField(_(Name), max_length
In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
in a django-tastypie app I have the following Django-models: class Car(models.Model): name=models.CharField('name',max_length=64) class CarTrack(models.Model):
I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
I have a django app with models as follows: A Question model An Answer
I have a simple test case failing in Django: Model (app/models.py): from django.db import
I have two related models (one to many) in my django app and When
I've got a basic Item model in my Django app: class Item(models.Model): name =
Lets say, I have following models in my Django app. class EventGroup name =
I have an existing app with the following model class Contact(models.Model): lastname = models.CharField(max_length=200)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.