Wagtail中的翻译说明不起作用

时间:2017-10-31 00:34:04

标签: python django internationalization django-migrations wagtail

尝试根据the instructions在wagtail中创建多个语言字段。

我的base.py文件包含:

MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware',
]
USE_I18N = True

我的blog/models.py文件如下所示:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailsearch import index

from django.utils import translation

class TranslatedField(object):
    def __init__(self, en_field, es_field):
        self.en_field = en_field
        self.es_field = es_field

    def __get__(self, instance, owner):
        if translation.get_language() == 'es':
            return getattr(instance, self.es_field)
        else:
            return getattr(instance, self.en_field)


class BlogIndexPage(Page):
    intro = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('intro', classname="full")
    ]

    subpage_types = ['blog.BlogPage']


class BlogPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)


    #body_en = RichTextField(blank=True)
    #body_es = RichTextField(blank=True)

    body = TranslatedField(
        'body_en',
        'body_es',
    )

    search_fields = Page.search_fields + [
        index.SearchField('intro'),
        #index.SearchField('body'),
    ]

    content_panels = Page.content_panels + [
        FieldPanel('date'),
        FieldPanel('intro'),
        #FieldPanel('body', classname="full"),
    ]

    parent_page_types = ['blog.BlogIndexPage']

当我运行./manage.py makemigrations时,它运行正常,但当我运行./manage.py migrate时,我收到错误:

django.db.utils.ProgrammingError: column "body_es" of relation "blog_blogpage" already exists

如何解决此错误?

- 更新1 -

我最近的两次迁移是:

# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-31 00:20
from __future__ import unicode_literals

from django.db import migrations


    class Migration(migrations.Migration):

        dependencies = [
            ('blog', '0004_auto_20171031_0017'),
        ]

        operations = [
            migrations.RemoveField(
                model_name='blogpage',
                name='body_en',
            ),
            migrations.RemoveField(
                model_name='blogpage',
                name='body_es',
            ),
        ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.5 on 2017-10-31 00:17
from __future__ import unicode_literals

from django.db import migrations
import wagtail.wagtailcore.fields


class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0003_auto_20171030_2353'),
    ]

    operations = [
        migrations.AddField(
            model_name='blogpage',
            name='body_en',
            field=wagtail.wagtailcore.fields.RichTextField(blank=True),
        ),
        migrations.AddField(
            model_name='blogpage',
            name='body_es',
            field=wagtail.wagtailcore.fields.RichTextField(blank=True),
        ),
    ]

0 个答案:

没有答案