How to properly rename a wagtail page model - django

I have two models in a wagtail app, PageType and NewPageType, and need to replace PageType with NewPageType.
I thought I could remove PageType from my models.py and then run a migration to remove it, and then rename NewPageType to PageType and run a second migration.
However, I'm running into errors when I do this:
[2019-01-22 23:20:26,344] [ERROR] Internal Server Error: /cms/
Traceback (most recent call last):
File "/.../python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/.../python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
[...snip...]
File "/.../python3.6/site-packages/django/db/models/query.py", line 1121, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/.../python3.6/site-packages/wagtail/core/query.py", line 397, in specific_iterator
yield pages_by_type[content_type][pk]
KeyError: 278
It seems like something didn't get updated automatically by Django's built-in migration handling. I couldn't tell what steps I'm missing here so would love to get some help. Thanks!

That's because Wagtail pages uses Multi-table inheritance and part of your deleted PageType pages are still around.
Let's take a look at a fresh install of Wagtail (i.e. wagtail start mysite) which comes with a home.HomePage model and creates one HomePage by default. We can have a look at the database and confirm that there is indeed an entry in the database:
sqlite> SELECT * FROM home_homepage;
page_ptr_id
3
However it's rather empty. There's no title, no path, nothing but a page_ptr_id. This is because the HomePage inherit from the Page model which isn't abstract. Therefore, there is a database table for that Page model as well (this is how Multi-table inheritance works with Django). Let's have a look at the corresponding table (voluntarily ommitting some columns) :
sqlite> SELECT id, path, title, slug, url_path, content_type_id FROM wagtailcore_page;
id|path |title|slug|url_path|content_type_id
1 |0001 |Root |root|/ |1
3 |00010001|Home |home|/home/ |2
Here it is!
Similarly, in your case, there is the wagtailcore_page, the myapp_pagetype and myapp_newpagetype tables. By deleting the PageType model, django created a migration which then deleted the myapp_pagetype but left the entry in the wagtailcore_page table. Therefore now, when you load the admin interface, Wagtail tries to load the page #3 but fail to do so.
For that reason, before deleting a Page model, you need to delete all the pages first. You can achieve this by adding a RunPython step to your migration.
You would still be left with renaming your second model though which can be difficult with Django, although if you're lucky, renaming it in your models.py file and runing makemigrations might be enough for Django to detect that it should rename the model. If not, or if you have relationships which need to be renamed to, it might be more involed, see 1 and 2.

To recover from it and be able to load admin again, do the following:
Delete the reference:
import django
django.setup()
from wagtail.core.models import PageRevision
PageRevision.objects.filter(page_id= 278).delete()
exit()
Then delete the page.
django-admin dbshell
DELETE FROM wagtailcore_page WHERE id=278;
Hope that helps.

I'm new to wagtail, but I had no issues renaming the model and the correlating template, then running
python manage.py makemigrations
python manage.py migrate
That being said, I was not reusing an old name like OP. I might recommend anyone having this issue to come up with a new name for the model and make it a descriptive one.

Related

'something here' matching query does not exist

I created a web app and deployed on Heroku successfully even database migrations. When I open the app I see the error like this:
'something here' matching query does not exist.
App URL: https://lp7.herokuapp.com/lp7/
App isn't working and if I remove this data feild from model, then app works but no single data is coming from database. But, when I go to heroku database it shows:
No. of Tables = 28
No. Rows = 220
Size of data = 9.4Mb
It means, the all migrations exists on heroku but not showing on website.
Any solution..?
You need to update the table for model Topbar in Heroku. You can use admin-site to update it.
Also, for future, you might want to change from:
num = TopBar.objects.get()
to
num = TopBar.objects.last()
So it will return the last object created in queryset. It will return None if no object has been created for TopBar
Looking at the traceback https://lp7.herokuapp.com/lp7/ it is bad here
num = TopBar.objects.get() you should pass something matching
if you dont need it do it like
try:
num = TopBar.objects.get(id=1)
except TopBar.DoesNotExist:
pass
#handle if not found logic here
For migrating data from your local database to heroku database is to run:
python manage.py dumpdata yourapp > yourapp/fixtures/app_data.json
Then you need to commit this file to heroku branch, for example:
git commit heroku main
After commiting run the following command to load data into heroku database:
heroku run python manage.py loaddata app_data

How to extend Django's test database with custom SQL view?

I'm using Django 2.1 with MySQL.
I have one custom SQL view, which is bound with a model with Meta managed = False. Django's TestCase has no idea how the view is created, so I'd like to provide SQL command to create this view. The best option would be to do this on database create, but I have no idea how to do that.
What I've done so far was to override TestCase's setUp method. It looks like that:
class TaskDoneViewTest(TestCase):
def setUp(self):
"""
Create custom SQL view
"""
cursor = connection.cursor()
file_handle = open('app/tests/create_sql_view.sql', 'r+')
sql_file = File(file_handle)
sql = sql_file.read()
cursor.execute(sql)
cursor.close()
def test_repeatable_task_done(self):
# ...
def test_one_time_task_done(self):
# ...
I've got this solution from similar SO post: How to use database view in test cases. It would be a nice temporary solution, but the problem is with all those 2 test cases active I'm getting following error:
$ python manage.py test app.tests
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
...E..
======================================================================
ERROR: test_repeatable_task_done (app.tests.test_views.TaskDoneViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/asmox/AppDev/Python/bubblechecklist/project_root/app/tests/test_views.py", line 80, in setUp
cursor.execute(sql)
File "/home/asmox/AppDev/Python/bubblechecklist/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/asmox/AppDev/Python/bubblechecklist/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/asmox/AppDev/Python/bubblechecklist/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in _execute
self.db.validate_no_broken_transaction()
File "/home/asmox/AppDev/Python/bubblechecklist/env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 437, in validate_no_broken_transaction
"An error occurred in the current transaction. You can't "
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.
For some reason this error doesn't happen when I have only one test case active (why?).This error remains until I change my test's base class from TestCase to TransactionTestCase.
Well, I would ask why this happen and if there is any solution to get it okay with simple TestCase class, because my test for now has nothing to do with transactions and I find this working solutions a bit too dirty, but...
I would more likely stick to the main issue, that is, to globally (for all my test cases) do the following thing:
When testing database is created, do one more custom SQL from provided file. It is going to create required view
Can you please help me how to do that?
If you read the documentation for TestCase, you'll see that it wraps each test in a double transaction, one at the class level and one at the test level. The setUp() method runs for each test and is thus inside this double wrapping.
As shown in the above mentioned docs, it is suggested you use setUpTestData() to set up your db at the class level. This is also where you'd add initial data to your db for all your tests to use.

OperationalError1075 'Incorrect table definition'

I changed one of my models to go from
serial_number = models.BigIntegerField(unique=True)
to
serial_number = models.AutoField(primary_key=True)
and ran
$ python manage.py check -v 3
$ python manage.py makemigrations -v 2
$ python manage.py migrate -v 2
However, when I try to run the unit tests (which passed fine before), I get this error:
[...]
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue django.db.utils.OperationalError: (1075, 'Incorrect table definition; there can be only one auto column and it must be defined as a key')
How to resolve this?
The Error1075 'Incorrect table definition' came from Mysql, not Django. That is because the table u changed got 2 auto_created fields, while it is only one field alled allow for one table in Mysql.
In Django, just add a parm 'auto_created':
serial_number = models.AutoField(primary_key=True, auto_created=False)
then remove u old migrations file last time and make migrations again, continue migrate will be ok.
So far the only "solution" was to trash all the migrations and re-create the database. Thus trashing all the old data. But it is less of a pain than fixing this craziness.

How to resolve "django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: foo" in Django 1.7?

On upgrading to Django 1.7 I'm getting the following error message from ./manage.py
$ ./manage.py
Traceback (most recent call last):
File "./manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "/home/johnc/.virtualenvs/myproj-django1.7/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
utility.execute()
File "/home/johnc/.virtualenvs/myproj-django1.7/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 391, in execute
django.setup()
File "/home/johnc/.virtualenvs/myproj-django1.7/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/johnc/.virtualenvs/myproj-django1.7/local/lib/python2.7/site-packages/django/apps/registry.py", line 89, in populate
"duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: foo
What's the problem and how do I resolve it?
The problem is that with the changes to apps in Django 1.7, apps are required to have a unique label.
By default the app label is the package name, so if you've got a package with the same name as one of your app modules (foo in this case), you'll hit this error.
The solution is to override the default label for your app, and force this config to be loaded by adding it to __init__.py.
# foo/apps.py
from django.apps import AppConfig
class FooConfig(AppConfig):
name = 'full.python.path.to.your.app.foo'
label = 'my.foo' # <-- this is the important line - change it to anything other than the default, which is the module name ('foo' in this case)
and
# foo/__init__.py
default_app_config = 'full.python.path.to.your.app.foo.apps.FooConfig'
See https://docs.djangoproject.com/en/1.7/ref/applications/#for-application-authors
I found simple solution for this. In my case following line is added twice under INSTALLED_APPS,
'django.contrib.foo',
Removed one line fixes the issue for me.
I had the same error - try this:
In INSTALLED_APPS, if you are including 'foo.apps.FooConfig', then Django already knows to include the foo app in the application, there is therefore no need to also include 'foo'.
Having both 'foo' and 'foo.apps.FooConfig' under INSTALLED_APPS could be the source of your problem.
Well, I created a auth app, and included it in INSTALLED_APP like src.auth (because it's in src folder) and got this error, because there is django.contrib.auth app also. So I renamed it like authentication and problem solved!
I got the same problem.
Here my app name was chat and in the settings.py , under installed apps i have written chat.apps.ChatConfig while i have already included the app name chat at the bottom. When i removed the chat.apps.ChatConfig mine problem was solved while migrations. This error may be due to the same instance that you might have defined you app name foo twice in the settings.py. I hope this works out!!
please check if anything is duplicated in INSTALLED_APPS of settings.py
This exception may also be raised if the name of the AppConfig class itself matches the name of another class in the project. For example:
class MessagesConfig(AppConfig):
name = 'mysite.messages'
and
class MessagesConfig(AppConfig):
name = 'django.contrib.messages'
will also clash even though the name attributes are different for each configuration.
In previous answer 'django.contrib.foo', was mentioned, but basically adding any app twice can cause this error just delete one (Django 3.0)
for me it was in settings.py
INSTALLED_APPS = [
...
'accounts.apps.AccountsConfig',
'accounts.apps.AccountsConfig',
...
]
just delete one of them
Basically this problem has been created due to duplication of name of installed app in the settings:
This is how I resolved the problem. In settings.py file:
Check the install app in the setting.py if the install app are duplicate
Error shown due to duplication of app name
Remove the duplicate name in the install file
After problem is resolved, you will see interface in your screen
For this I have created application name as polls instead of foo
As therefromhere said this is a new Django 1.7 feature which adds a kind of “app registry” where applications must be determined uniquely (and not only having different python paths).
The name attribute is the python path (unique), but the label also should be unique. For example if you have an app named 'admin', then you have to define the name (name='python.path') and a label which must be also unique (label='my admin' or as said put the full python path which is always unique).
Had same issue, read through the settings.py of the root folder, removed any INSTALLED APPS causing conflict... works fine. Will have to rename the apps names
Need to check in two file
1- apps.py
code something like
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class ModuleConfig(AppConfig):
name = "ocb.module_name"
verbose_name = _("Module Name")
2 - init.py
code something like
default_app_config = "ocb.users.apps.ModuleConfig"
default_app_config is pointed to your apps.py's class name
in my case, in mysite settings.py , in INSTALLED_APPS array variable I put the name of the app twice by mistake.
I had almost the same issue.
```File "/Users/apples/.local/share/virtualenvs/ecommerce-pOPGWC06/lib/python3.7/site-packages/django/apps/registry.py", line 95, in populate
"duplicates: %s" % app_config.label)
django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: auth```
I had installed Django.contrib.auth twice. I removed one and it worked well.
From my experience, this exception was masking the real error. To see the real error (which in my case was an uninstalled python package) comment out the following in django/apps/registry.py:
if app_config.label in self.app_configs:
# raise ImproperlyConfigured(
# "Application labels aren't unique, "
# "duplicates: %s" % app_config.label)
pass
Check for duplicates in INSTALLED_APPS inside the settings.py...If so remove one of it and rerun the command
I had Django==3.2.9 when tried to test my existing Django app on a new environment. I had this exact issue and fixed it by downgrading to Django==3.1.13.
There seems to be an update to applications, check the Django 3.2 documentation for detailed information.
This error occurs because of duplication in your INSTALLED_APPS in settings.py file which is inside your project.
For me, the problem was that I had copy-pasted entire app instead of creating it using command line. So, the app name in the apps.py file was same for 2 apps. After I corrected it, the problem was gone.
In case if you have added your app name in settings.py
example as shown in figure than IN settings.py Remove it and Try this worked for me.
give it a try .
This Worked Because settings.py assumes installing it twice and does not allow for migration
If you want to back older version, command
pip install django==1.6.7

autodoc failing on django ImageField

I'm attempting to use sphinx to document the django app I'm writing. So far I have my code.rst setup to look at models.py, and when I run make html I get the automatic documentation, however I get a warning:
WARNING: autodoc can't import/find attribute 'myapp.models.MyModel.image', it reported error: "image", please check your spelling and sys.path
The entire tracelog is:
Traceback (most recent call last):
File "C:\blah\lib\site-packages\sphinx\ext\autodoc.py", line 326, in import_object
obj = self.get_attr(obj, part)
File "C:\blah\lib\site-packages\sphinx\ext\autodoc.py", line 232, in get_attr
return safe_getattr(obj, name, *defargs)
File "C:\blah\lib\site-packages\sphinx\util\inspect.py", line 70, in safe_getattr
raise AttributeError(name)
AttributeError: image
MyModel.image is an image field, simply on the model defined as:
#: image file location for ``MyModel``
image = models.ImageField(upload_to="images/")
If I change image to models.Charfield for example it runs fine. Is there any reason why an ImageField would cause sphinx issues?
my code.rst:
.. automodule:: dynamicbanners.models
:members:
So after reading the comments it seems this is a quirk of Django that messes with sphinx.ext.autodoc. Hopefully a fix will be dropped into Django 1.6 soon, or failing that I think the upcoming 1.2 version of Sphinx might provide a way around it, but for now I'll need to find some sort of workaround.
Looks like this was fixed in Django 1.8 - https://code.djangoproject.com/ticket/12568