向 Django FlatPage 代理模型添加一个字段
Add a field to a Django FlatPage proxy model
这是我的 models.py:
from django.contrib.flatpages.models import FlatPage
class FlatPageManager(models.Manager):
def search(self, query=None):
qs = self.get_queryset()
if query is not None:
or_lookup = (Q(title__icontains=query) |
Q(content__icontains=query)
)
qs = qs.filter(or_lookup).distinct()
return qs
class MyFlatPage(FlatPage):
class Meta:
proxy = True
published = models.DateTimeField() # <- this is the field I want to add
objects = FlatPageManager()
当我执行 makemigrations
时出现此错误:
./manage.py makemigrations
SystemCheckError: System check identified some issues:
ERRORS:
?: (models.E017) Proxy model 'MyFlatPage' contains model fields.
我做错了什么?我想做的就是添加一个 published
字段,这样我就可以在将来的某个日期只显示 post。
编辑
我现在了解到,根据定义,代理模型不接受新字段。那么我的下一个问题是:我怎样才能 'convert' 我现有的代理模型到其他东西(当然不会丢失我现有的数据)以便我可以有一个额外的 published
字段?
如果设置 proxy=True
,则可以 adjust/enhance Python 级别的行为。您不能向代理模型添加其他字段。但是,您可以像以前一样设置自定义对象管理器以及函数或方法。
take a look a model-inheritance #3
您可以将默认行为添加到 published
class FlatPage(models.Model):
published = models.DateTimeField(auto_now_add=True)
...
第一次更新
如果您使用新字段扩展 FlatPage
,您将不会丢失数据。如果您想保存数据,请致电 python manage.py dumpdata > db.json
第二次更新
models.py
from django.contrib.flatpages.models import FlatPage
from django.db import models
class MyCustomFlatePage(FlatPage):
published = models.DateTimeField(auto_now_add=True)
forms.py
from django.contrib.flatpages.forms import FlatpageForm
class MyFlatPageForm(FlatpageForm):
class Meta:
model = MyFlatPage
fields = '__all__'
admin.py
class MyFlatPageAdmin(FlatPageAdmin):
form = MyFlatPageForm
fieldsets = (
(None, {'fields': ('url', 'title', 'content', 'published', 'sites')}),
(_('Advanced options'), {
'classes': ('collapse',),
'fields': (
'enable_comments',
'registration_required',
'template_name',
),
}),
)
admin.site.unregister(FlatPage)
admin.site.register(MyFlatPage, MyFlatPageAdmin)
这是我的 models.py:
from django.contrib.flatpages.models import FlatPage
class FlatPageManager(models.Manager):
def search(self, query=None):
qs = self.get_queryset()
if query is not None:
or_lookup = (Q(title__icontains=query) |
Q(content__icontains=query)
)
qs = qs.filter(or_lookup).distinct()
return qs
class MyFlatPage(FlatPage):
class Meta:
proxy = True
published = models.DateTimeField() # <- this is the field I want to add
objects = FlatPageManager()
当我执行 makemigrations
时出现此错误:
./manage.py makemigrations
SystemCheckError: System check identified some issues:
ERRORS:
?: (models.E017) Proxy model 'MyFlatPage' contains model fields.
我做错了什么?我想做的就是添加一个 published
字段,这样我就可以在将来的某个日期只显示 post。
编辑
我现在了解到,根据定义,代理模型不接受新字段。那么我的下一个问题是:我怎样才能 'convert' 我现有的代理模型到其他东西(当然不会丢失我现有的数据)以便我可以有一个额外的 published
字段?
如果设置 proxy=True
,则可以 adjust/enhance Python 级别的行为。您不能向代理模型添加其他字段。但是,您可以像以前一样设置自定义对象管理器以及函数或方法。
take a look a model-inheritance #3
您可以将默认行为添加到 published
class FlatPage(models.Model):
published = models.DateTimeField(auto_now_add=True)
...
第一次更新
如果您使用新字段扩展 FlatPage
,您将不会丢失数据。如果您想保存数据,请致电 python manage.py dumpdata > db.json
第二次更新
models.py
from django.contrib.flatpages.models import FlatPage
from django.db import models
class MyCustomFlatePage(FlatPage):
published = models.DateTimeField(auto_now_add=True)
forms.py
from django.contrib.flatpages.forms import FlatpageForm
class MyFlatPageForm(FlatpageForm):
class Meta:
model = MyFlatPage
fields = '__all__'
admin.py
class MyFlatPageAdmin(FlatPageAdmin):
form = MyFlatPageForm
fieldsets = (
(None, {'fields': ('url', 'title', 'content', 'published', 'sites')}),
(_('Advanced options'), {
'classes': ('collapse',),
'fields': (
'enable_comments',
'registration_required',
'template_name',
),
}),
)
admin.site.unregister(FlatPage)
admin.site.register(MyFlatPage, MyFlatPageAdmin)