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

  • Home
  • SEARCH
  • 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 8565803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:30:43+00:00 2026-06-11T17:30:43+00:00

I have these classes in the models.py of my django application.I am using postgresql

  • 0

I have these classes in the models.py of my django application.I am using postgresql 8.3 as database

class Course(models.Model):
    students = models.ManyToManyField(User)
    title = models.CharField(max_length=200)
    description = models.TextField()

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name_plural="Courses"

class CourseChoicesForm(forms.Form):
    courseoption = forms.ChoiceField(choices=[(x.id,x.title) for x in Course.objects.all()])

When I run python manage.py syncdb ,I get an error which says the relation course is not found

...
File "/home/me/Django-1.4/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/me/dev/python/django/myprj/myapp/models.py", line 50, in <module>
    class CourseChoicesForm(forms.Form):
  File "/home/me/dev/python/django/myprj/myapp/models.py", line 52, in CourseChoicesForm
    courseoption = forms.ChoiceField(choices=[(x.id,x.title) for x in Course.objects.all()])
...
File "/home/me/Django-1.4/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: relation "myapp_course" does not exist

UPDATE:
I solved this as follows

class CourseChoicesForm(forms.Form):
    courseoption = forms.ChoiceField(choices=[],required=False)
    def __init__(self, *args, **kwargs):
        super(CourseChoicesForm, self).__init__(*args, **kwargs)
        self.fields['courseoption'].choices = [(x.id,x.title) for x in Course.objects.all()]

Still,I am not quite sure why this behaviour occurred.Can someone please explain?

  • 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-06-11T17:30:44+00:00Added an answer on June 11, 2026 at 5:30 pm

    It’s not good practise to put your forms inside your models.py file, and this is exactly why.

    For syncdb to run, it first needs to load your models files. This certainly has to happen before it can make any database updates.

    Now, your models.py file also includes a form class, and that class definition relies on the table being present. (That’s just part of the way that Python works — your class definitions are executed at the time the module is imported). So, before the table could be added, syncdb had to load a module which required that the table be present.

    When you changed your form class, you moved the line that depended on the new table — now it’s inside an __init__ method. Unlike the class definition, that method does not get run at import time. It only runs when you actually create a form object. Now syncdb can import your new module and update the database, and everything works as it should.

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

Sidebar

Related Questions

I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True)
I have following setup. from django.db import models from django.contrib.auth.models import User class Event(models.Model):
Here's an example: If I have these classes class Author(models.Model): name = models.CharField(max_length=45) class
I use django, and have a lengthy models.py file. class Foo(models.Model): name = models.CharField()
I am using the standard User model (django.contrib.auth) which comes with Django. I have
I have two models in Django for a 2d map based game: class Block(models.Model):
I am using a django DateField in my model. class CalWeek(models.Model): start_monday = models.DateField(verbose_name=Start
I have two Django model classes that are structured similar to the following: class
I have these 3 models in models.py class Customer(models.Model): name = models.CharField(max_length=50) .... class
I have 2 multi-table inherited models like these: class Post(models.Model): title = models.CharField(max_length=100, blank=True,

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.