I'm using django-wordpress to import some existing content into a Wordpress installation, and I've got a management command that creates Post
objects using the ORM (and I've set wordpress.models.READ_ONLY = False
).
I've got something like:
post = Post()
post.title = '...'
post.author = user # I've created a wordpress.models.User object
... # additional content & required fields (e.g. the DateTimeFields), but I omit the parent field
post.save()
At this point, I get a Traceback like the following. It appears that the parent
field is required, even tho it is defined with null=True
.
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File ".venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File ".venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File ".venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File ".venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "myapp/management/commands/export_blog_to_wordpress.py", line 22, in handle
self.export_posts()
File "myapp/management/commands/export_blog_to_wordpress.py", line 160, in export_posts
wp.save() # We need the id before we can assign Categories
File ".venv/local/lib/python2.7/site-packages/wordpress/models.py", line 81, in save
super(WordPressModel, self).save(**kwargs)
File ".venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 463, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File ".venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 551, in save_base
result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File ".venv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 203, in _insert
return insert_query(self.model, objs, fields, **kwargs)
File ".venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 1576, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File ".venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 910, in execute_sql
cursor.execute(sql, params)
File ".venv/local/lib/python2.7/site-packages/django/db/backends/util.py", line 40, in execute
return self.cursor.execute(sql, params)
File ".venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 114, in execute
return self.cursor.execute(query, args)
File ".venv/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 201, in execute
self.errorhandler(self, exc, value)
File ".venv/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError: (1048, "Column 'post_parent' cannot be null")
Any ideas what's going on, here, and is this a bug or am I just doing something wrong?
My current workaround is this:
- Insert a dummy post with no content directly into the database:
insert into wp_posts (ID) values (1);
- Set the following:
post.parent = Post.objects.get(id=1)
before calling post.save()
Thanks in advance!