GreedyAi
GreedyAi

Reputation: 2839

App_Model does not exist in django

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

Upvotes: 0

Views: 856

Answers (1)

Hamish
Hamish

Reputation: 23316

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.

Upvotes: 2

Related Questions