Django Migrating Db Django.db.utils.programmingerror: Relation "django_site" Does Not Exist
Solution 1:
The ProgrammingError is a database error. It means that the table you are attemping to read is not ready, because you attemp to acces to it before it is created:
classLatestBlogEntries(Feed):
current_site = Site.objects.get_current() <---- this line
You shoud load the static current_site variable in an independent function using post_migrate signal. There is an example in the docs
In case you find this overkill, there are workarrounds like:
- Catch the error. This will allow you to migrate, and in the next restart of the application it will read the object flawlessly.
from django.db import ProgrammingError
classLatestBlogEntries(Feed):
try:
current_site = Site.objects.get_current()
except ProgrammingError:
pass
- Loading the variable in _init_ method if not defined.
This usually happens when this operation is modeled with the tables already created, and then the database is cleared to start from scratch.
Solution 2:
You shouldn't call Site.objects.get_current()
in the LatestBlogEntries
class. It requires a database lookup, which causes the relation does not exist
error when you haven't run the initial migrations yet.
You can use methods for title
and description
. This way, the Site.objects.get_current()
runs when the feed is accessed, not when Django starts.
classLatestBlogEntries(Feed):deftitle(self, obj):
current_site = Site.objects.get_current()
return current_site.name + " - Latest News"
link = "/news/"defdescription(self, obj):
current_site = Site.objects.get_current()
return"Latest news and updates from " + current_site.domain
defitems(self):
return BlogEntry.objects.all()[:10]
defitem_title(self, item):
return item.title
defitem_description(self, item):
return item.description
Post a Comment for "Django Migrating Db Django.db.utils.programmingerror: Relation "django_site" Does Not Exist"