4

在我的 django-cms 网站中,通过管理界面发布页面时遇到了一些麻烦。在堆栈跟踪下方:

    DEBUG 2013-06-07 14:09:31,562 util 32222 140056576640768 (0.000) SELECT DISTINCT "menus_cachekey"."key" FROM "menus_cachekey" WHERE "menus_cachekey"."site" = 1 ; args=(1,)
DEBUG 2013-06-07 14:09:31,565 util 32222 140056576640768 (0.000) DELETE FROM "menus_cachekey" WHERE "menus_cachekey"."site" = 1 ; args=(1,)
DEBUG 2013-06-07 14:09:31,570 util 32222 140056576640768 (0.001) INSERT INTO "cms_page" ("created_by", "changed_by", "parent_id", "creation_date", "changed_date", "publication_date", "publication_end_date", "in_navigation", "soft_root", "reverse_id", "navigation_extenders", "published", "template", "site_id", "login_required", "limit_visibility_in_menu", "level", "lft", "rght", "tree_id", "publisher_is_draft", "publisher_public_id", "publisher_state") VALUES (E'roberto', E'roberto', NULL, E'2013-06-07 12:09:31.567872+00:00', E'2013-06-07 12:09:31.567989+00:00', E'2013-06-07 12:09:26.553334+00:00', NULL, false, false, NULL, NULL, true, E'unicms/departments_and_groups/department_detail.html', 1, false, NULL, 0, 1, 2, 9, false, 9, 0) RETURNING "cms_page"."id"; args=(u'roberto', u'roberto', None, u'2013-06-07 12:09:31.567872+00:00', u'2013-06-07 12:09:31.567989+00:00', u'2013-06-07 12:09:26.553334+00:00', None, False, False, None, None, True, u'unicms/departments_and_groups/department_detail.html', 1, False, None, 0, 1, 2, 9, False, 9, 0)
ERROR 2013-06-07 14:09:31,571 base 32222 140056576640768 Internal Server Error: /admin/cms/page/9/change-status/
Traceback (most recent call last):
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 202, in inner
    return view(request, *args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner
    return func(request, *args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/utils/decorators.py", line 21, in bound_func
    return func(self, *args2, **kwargs2)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/cms/admin/pageadmin.py", line 1062, in change_status
    success = method()
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/cms/models/pagemodel.py", line 419, in publish
    public_page.save()
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/cms/models/pagemodel.py", line 367, in save
    super(Page, self).save(**kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/mptt/models.py", line 696, in save
    super(MPTTModel, self).save(*args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/models/base.py", line 546, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/cms/models/pagemodel.py", line 384, in save_base
    ret = super(Page, self).save_base(*args, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/models/base.py", line 650, in save_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/models/manager.py", line 215, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/models/query.py", line 1661, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 937, in execute_sql
    cursor.execute(sql, params)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/backends/util.py", line 41, in execute
    return self.cursor.execute(sql, params)
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 56, in execute
    six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
  File "/home/roberto/.virtualenvs/ve_news2_app/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute
    return self.cursor.execute(query, args)
IntegrityError: duplicate key value violates unique constraint "cms_page_publisher_public_id_key"

部门模型很简单

class Department(models.Model):
    research_area = models.ManyToManyField('research_area.ResearchArea', blank=True,
                                           related_name='departments')  # many-to-many
    products = models.ManyToManyField('products_activities.Product', blank=True, related_name="departments")
    employees = models.ManyToManyField('staff.Person', through='department.PersonDepartmentMembership',
                                       related_name='departments')  # TODO WARNINGS
    research_groups = models.ManyToManyField('group.Group', blank=True, related_name='departments',
                                             through="department.GroupDepartmentMembership")
    page = models.OneToOneField(Page, blank=True, related_name='department', null=True)


    name = models.CharField(_('department name'), max_length=50, default=_('Department name'))
    description = models.TextField(_('department description'))
    logo = models.ImageField(upload_to='img/departments')
    address = models.CharField(_('office address'), max_length=255)
    post_address = models.CharField(_('post address'), max_length=255)
    telephone = PhoneNumberField(blank=True, null=True)
    fax = PhoneNumberField(blank=True, null=True)
    email = models.EmailField()

    class Meta:
        permissions = (
            ('view_department', _('View department')),
        )

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('pages-details-by-slug', kwargs={'slug': self.page.get_slug('en')})

并非所有页面都受到此问题的影响。在我的 cms 中,我设置了一个自动页面创建系统。当用户创建部门时,会设置一个新页面。

@receiver(post_save, sender=Department)
def _on_save_department_create_link_to_page(sender, instance, created, **kwargs):
    from cms.api import assign_user_to_page, add_plugin, publish_page
    if created:  # create department Page
        from cms.api import create_page

        instance.page = create_page(
            title=instance.name,
            language='en',
            published=False,
            template=settings.CMS_TEMPLATE_DEPARTMENT[0],
        )

        # add some plugins...

        editors = instance.employees.filter(cmsuser__groups__name__exact='web')
        for e in editors:
            assign_user_to_page(instance.page, e)
        instance.page.save()

我在互联网上找到了一些博客文章,指出他可能是由于连续的南迁。我几乎不怀疑这一点,因为我在没有应用任何数据迁移的情况下也出现了错误。

任何反馈将不胜感激!提前致谢 :-)

4

2 回答 2

2

由于升级 django-cms 时审核方式发生了变化,您需要运行:

python manage.py cms moderator on

有关更多信息,请参阅http://docs.django-cms.org/en/2.4.0/advanced/cli.html#moderation-commands

如果您仍在为某些仍然给出该约束错误的页面而苦苦挣扎,则需要打开一个 django shell:

python manage.py dbshell

然后用SQL删除问题

delete from cms_page where publisher_public_id=79;
于 2013-06-29T12:13:16.823 回答
1

您的数据库表cms_page中有一个名为cms_page_publisher_public_id_key. 约束可以指定很多东西,但通常用于保持值的唯一性。在您的情况下,它似乎也具有独特性:

duplicate key value violates unique constraint

在您的模型中,我看不到您指定为唯一的任何内容,但也许您随着时间的推移更改了代码并忘记相应地更改您的数据库表?

检查您的表cms_page在数据库中的样子以及约束cms_page_publisher_public_id_keyexaclty 的作用。然后你可能会找到原因。

于 2013-06-07T14:58:53.827 回答