在Mezzanine博客文章中添加一个字段

时间:2013-03-11 17:42:38

标签: django mezzanine

我正在使用Mezzanine进行项目。我需要在Mezzanine blogpost上添加一个额外的字段。

我注意到使用EXTRA_MODEL_FIELDS可以做到这一点,但看起来很复杂。

我也尝试将blog-package从site-package复制到我的项目路径,然后修改models.py。但我不行。

我是Django的新手,有人可以帮忙吗?

由于

2 个答案:

答案 0 :(得分:1)

By do some research, now I got the answer: 
1. copy the blog app from sites-package to my project 
2. change my setting.py 
   INSTALLED_APPS = (
    "blog",     #it was "mezzanine.blog",
    .....
3. modify the blog/models.py
   add following line to class BlogPost
    shop_url= models.CharField(max_length=250,null=True, blank=True)
4. migirate the table (installed South)
  ./manage.py schemamigration blog --auto
  ./manage.py migrate blog

答案 1 :(得分:0)

您可以创建一个django应用程序(CustomBlog),将其添加到已安装的应用程序中 并删除或评论Mezzanine博客:

INSTALLED_APPS = (
    "CustomBlog",     #it was "mezzanine.blog",
     ...
)

在CustomBlog的models.py和admin.py中,继承自Mezzanine的BlogPost类:

models.py
from django.db import models
from mezzanine.blog.models import BlogPost
from mezzanine.blog.models import BlogCategory


class CustomBlog(BlogPost):
    # Add New Field
    # example 
    new_field = models.CharField(max_length=255)

class CustomBlogCategory(BlogCategory):
    pass

admin.py
from django.contrib import admin
from .models import CustomBlog,CustomBlogCategory


admin.site.register(CustomBlog)
admin.site.register(CustomBlogCategory)

然后在终端创建并运行迁移

python manage.py makemigrations
python manage.py migrate