0

I am new to django and have a bit of a problem. I was creating a simple view, template and models for small project. I was getting error that Vote_Type does not exist, when I was asking for Type model in 'Vote' app. I don't get why is it looking for Vote_Type instead of Type in database.

So I went to look for a problem and now that I tried to create a object of model Type in database from python console I got the same error.

This is my models:

from django.db import models
from django.utils import timezone

# Create your models here.
class Voted_Object(models.Model):
    Voted_Object_name = models.CharField(max_length=50)
    Voted_Object_image = models.ImageField
    Voted_Object_rating = models.IntegerField(default=0)
    Voted_Object_Id = models.IntegerField

class Type(models.Model):
    pub_date = models.DateTimeField('date published')
    Type_name = models.CharField(max_length=50)
    Type_Id = models.IntegerField

and this is the error I get when I try to do Type.objects.all() in python console.

relation "Vote_type" does not exist LINE 1: ...te_type"."pub_date", "Vote_type"."Type_name" FROM "Vote_type..

What is wrong with my code? why is django looking for Vote_Type instead of Type

2
  • 1
    Have you run makemigrations and migrate? Commented Jun 26, 2015 at 9:24
  • I am sure I have, but let me check do it again. Commented Jun 26, 2015 at 9:32

1 Answer 1

2

By default Django creates tables with the naming scheme "{app_name}_{class_name}" - so it is expecting to find a table called "Vote_type" in the database that corresponds to the Type model in the Vote app. Did you create and run migrations to add the table, or do this manually?

Unrelated, but can I suggest you structure you app like this:

from django.db import models
from django.utils import timezone

# Create your models here.
class Vote(models.Model):
    name = models.CharField(max_length=50)
    image = models.ImageField()
    rating = models.IntegerField(default=0)

class VoteType(models.Model):
    pub_date = models.DateTimeField('date published')
    name = models.CharField(max_length=50)
  • You don't need id fields - they're added automatically
  • Prefixing field names just makes things verbose. vote.name is much easier to write/understand than vote.Voted_Object_name. Same for the _Object suffix.
  • type is a keyword, so maybe use VoteType instead.
Sign up to request clarification or add additional context in comments.

2 Comments

Thx for suggestions! I am sure I have run migrations, but let me do it again just to make sure.
yep, it worked. Sorry for silly question I was sure I made migrations. I think that I forgot to do migrate.

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.