Here is the exact error I am getting:
django.db.utils.ProgrammingError: relation "blog_blogtype" does not exist
LINE 1: ..."blog_blogtype"."id", "blog_blogtype"."type" FROM "blog_blog...
I have created the tables in a database in Postgres but when I try to migrate Django returns this error. My models are below. I created the column referencing to the BlogType model "type" column. I do not know what else to put here. If needed please comment it, so I can add. Thank you in advance.
class BlogType(models.Model):
id = models.AutoField(primary_key=True)
type = models.CharField(max_length=100)
def __str__(self):
return self.type
class BlogPost(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=200)
post_body = models.TextField()
time = models.DateTimeField()
category = models.ForeignKey(BlogType, related_name="type", on_delete=models.CASCADE)
def __str__(self):
return self.title
EDIT: I have found that this line in my views.py is causing the problem. I still cannot figure out how to fix it.
blog_post_types = list(BlogType.objects.all())
Related
I am trying to query from backward: at fist see my models:
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=100, unique=True)
body = models.TextField()
category = models.ForeignKey('blog.Category', on_delete=models.CASCADE)
def __unicode__(self):
return '%s' % self.title
class Category(models.Model):
name = models.CharField(max_length=100, db_index=True)
I have many category and many post, one category name is tech I am trying to get all the post those are in tech category.
I tried like this. Category.objects.filter(contain__exact='tech') but it is not work anymore.
Can anyone help me to figure get it done?
Best way to get all the post in tech category using foreign key.
tech_blogs = Blog.objects.filter(category__name__icontains='tech')
and also change
category = models.ForeignKey('Category', on_delete=models.CASCADE)
I have made changes to one of my models in my project and migrate, makemigrations does not work as expected. Rebuilding the database creates only 2 out of 3 tables from my models.py and i cannot figure out the problem.
There are two different apps; "blog" and "users". both are registered in the setting.py.
I completely removed the database and deleted the migrations folders.
then i tried the following stuff:
django makemigrations blog
django migrate blog
doing a global django makemigrations does not have any effect, no changes are detected.
here is the relevant models.py of "blog":
class Room(models.Model):
roomname = models.CharField(max_length=6, unique=True)
roomeditors=models.ManyToManyField(User,related_name='rooms_user_can_edit', blank=True)
displayadmin=models.ForeignKey(User,
related_name='room_user_is_displayadmin',null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.roomname
class Post(models.Model):
title = models.CharField(max_length=40)
content = models.TextField(max_length=300)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
rooms = models.ManyToManyField(Room, related_name='roomposts', through='Display')
def __str__(self):
return self.title
def get_absolute_url(self):
return "/post/{}/".format(self.pk)
class Display(models.Model):
class Meta:
auto_created = True
post = models.ForeignKey(Post, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
isdisplayed = models.BooleanField(default=0)
def __str__(self):
return str(self.isdisplayed)
every table gets created except from display. the output is:
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Room
- Create model Post
You are giving auto_created = True in your model's Meta class, which is not recommended neither its documented. Here is the list of all possible meta options you can give inside your model.
Official documentation says:
auto_created: Boolean flag that indicates if the field was automatically created, such as the OneToOneField used by model inheritance.
Giving this in Meta refrains Django to create this model itself.
When I go on localhost:8000/admin and click on "Quotes +ADD" it shows me error 500 instead of the editing interface. "Posts" works well. I just want to know if, without seeing the code, you could just tell me the different possible sources of this problem ?
EDIT: Here are models.py and admin.py:
models.py
class TimestampedModel(models.Model):
created = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)
class Meta:
abstract = True
# Create your models here.
class Post(TimestampedModel):
title = models.CharField(max_length = 255, default='')
intro = models.TextField(default='')
title_one = models.TextField(default='')
text_one = models.TextField(default='')
title_two = models.TextField(default='')
text_two = models.TextField(default='')
title_three = models.TextField(default='')
text_three = models.TextField(default='')
def __repr__(self):
return self.title
class Quote(models.Model):
quote = models.TextField(default='')
author = models.CharField(max_length = 60, default='')
def __repr__(self):
return self.quote
admin.py
from django.contrib import admin
from .models import Post, Quote
# Register your models here.
admin.site.register(Post)
admin.site.register(Quote)
I also would like to let you know that when I try to make migrations and then migrate, it says "No changes detected".
ProgrammingError at /admin/blog/quote/
relation "blog_quote" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "blog_quote"
I'll guess it might be OperationalError due to not running migrations for Quotes model.
Providing models.py and admin.py contents would make it much easier to debug though.
This is my model code
class Poll(models.Model):
created_at = models.DateTimeField(auto_now=True)
edited_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=200,default="X vs Y")
description = models.CharField(max_length=200,default="A poll")
def __str__(self):
return self.title
class Item(models.Model):
poll = models.ForeignKey('Poll',on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
edited_at = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=200)
type_of = models.CharField(max_length=200)
description = models.TextField(max_length=1200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.name
You see votes in Item model right. That's a problem. I use makemigrations migrate command. But I still get no such column error.
Edit:
This is makemigrations vs_chart output
Migrations for 'vs_chart':
vs_chart\migrations\0001_initial.py:
- Create model Item
- Create model Poll
- Add field poll to item
This is migrate command output.
Operations to perform:
Apply all migrations: vs_chart
Running migrations:
No migrations to apply.
Before you add field poll to item, you may try to provide default value for ForeignKey, absence of default value may cause this issue:
poll = models.ForeignKey('Poll',on_delete=models.CASCADE, default=0)
I've created (abridged) two tables:
class Tag(models.Model):
tag_word = models.CharField(max_length=35)
slug = models.CharField(max_length=250)
def __unicode__(self):
return self.word
Class Place(models.Model):
place_name = models.CharField(max_length=200)
...
tag_word = models.ManyToManyField(Tag)
I can see my Tags field on Django Admin, but not on PgAdmin and they don't return in my Place queryset. What have missed?
Thanks!
As pointed out by #karthikr in the comments, this data appears separately from the main table itself.
It is stored in another table and the name should be something like <appname>_place_tag_word for the above example. Or <appname>_<tablename>_<columnname> more generically.