0

前言:我正在编写自己的 Page 应用程序,该应用程序使用 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")

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

4

1 回答 1

1

代替

class ExtendedFlatPage(FlatPage, MPTTModel):

class ExtendedFlatPage(MPTTModel, FlatPage):

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

@评论

似乎 FlatPage 模型中的某些内容(属性、方法)会覆盖 MPTTModel 中的某些内容,从而导致此错误。

您从中导入的类的顺序很重要。这是一个例子:

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

C 类attribute值将是1.

于 2011-08-14T10:59:02.230 回答