扩展Django FlatPages以使用MPTT

时间:2011-08-08 16:34:19

标签: django django-mptt mptt django-flatpages

前言:我正在编写自己的使用MPTT和自定义页面模型的页面应用程序。这对我有用,但FlatPages比我的自定义页面模型更精致,所以我倾向于扩展它。

from django.db import models
from django.contrib.flatpages.models import FlatPage
from mptt.models import MPTTModel

class ExtendedFlatPage(FlatPage, MPTTModel):

    parent = models.ForeignKey('ExtendedFlatPage', null=True, blank=True, default=None, related_name="children", help_text="Hierarchical parent page (if any)")

    class Meta:
        ordering = ['flatpages__url']
        order_with_respect_to = 'parent'
        verbose_name = 'page'
        verbose_name_plural = 'pages'

    class MPTTMeta:
        left_attr = 'mptt_left'
        right_attr = 'mptt_right'
        level_attr = 'mptt_level'
        order_insertion_by = ['title']

    def __unicode__(self):
        return self.url

这几乎可行,但在我去运行python manage.py syncdb

时抛出错误

错误:

iMac:cms colab$ python manage.py syncdb
Creating tables ...
Creating table my_flatpages_extendedflatpage
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/core/management/commands/syncdb.py", line 101, in handle_noargs
    cursor.execute(statement)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Django-1.3-py2.7.egg/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/cursors.py", line 174, in execute
  File "build/bdist.macosx-10.6-intel/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
django.db.utils.DatabaseError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2")

如果有人能指出我正确的方向,我将非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

替换

class ExtendedFlatPage(FlatPage, MPTTModel):

class ExtendedFlatPage(MPTTModel, FlatPage):

这将允许MPTTModel类覆盖FlatPage属性和方法。

@comment

看起来FlatPage模型中的某些东西(属性,方法)会覆盖MPTTModel中存在此错误的内容。

您导入的课程顺序非常重要。这是一个例子:

class A:
   attribute = 1
class B:
   attribute = 2
class C(A,B):
   pass

C类attribute值为1