Django、article和categories的迁移非常完美,但在出现错误之后”;关系不';t出口“;
我正在创建一个包含文章等的新闻页面 因此,一篇文章有一个来源(类别)。然后多篇文章可以有相同的来源或多个 我把它分为两类:Django、article和categories的迁移非常完美,但在出现错误之后”;关系不';t出口“;,django,Django,我正在创建一个包含文章等的新闻页面 因此,一篇文章有一个来源(类别)。然后多篇文章可以有相同的来源或多个 我把它分为两类: @version_controlled_content @python_2_unicode_compatible class ArticleSource(TranslatableModel): taints_cache = True date_created = models.DateTimeField(_('created at'), auto_now_add=
@version_controlled_content
@python_2_unicode_compatible
class ArticleSource(TranslatableModel):
taints_cache = True
date_created = models.DateTimeField(_('created at'), auto_now_add=True)
date_modified = models.DateTimeField(_('modified at'), auto_now=True)
position = models.PositiveIntegerField(default=0, blank=False, null=False)
translations = TranslatedFields(
name=models.CharField(_('name'), max_length=510),
slug=models.SlugField(_('slug'), blank=True, db_index=True),
meta={'unique_together': (('language_code', 'slug'),)},
)
objects = TranslationManager()
class Meta:
verbose_name = _('Press source')
verbose_name_plural = _('Press sources')
ordering = ('position', )
@property
def count(self):
return self.press_article.filter(publish=True).count()
def __str__(self):
name = self.safe_translation_getter('name', any_language=True)
return name if name is not None else '(not translated)'
def save(self, *args, **kwargs):
super(ArticleSource, self).save(*args, **kwargs)
for lang in self.get_available_languages():
self.set_current_language(lang)
if not self.slug and self.name:
self.slug = slugify(force_text(self.name))
self.save_translations()
@version_controlled_content
@python_2_unicode_compatible
class Article(ModelMeta, TranslatableModel):
taints_cache = True
"""
Press article element,
"""
date_created = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
date_realization = models.DateField(_('Realised in'),
default=timezone.now)
image = FilerImageField(verbose_name=_('Featured image'), blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='image_press_article',
help_text=_('Set if the article will be featured'))
sources = models.ManyToManyField(ArticleSource, verbose_name=_('Source'),
blank=False, null=True, related_name='sources_press_article')
regions = models.ManyToManyField(Country, verbose_name=_('Country of the article'),
blank=True, null=True,
related_name='regions_press_article')
global_regions = models.BooleanField('Global', default=False)
featureArticle = models.BooleanField('Feature this article', default=False)
sites = models.ManyToManyField(Site, verbose_name=_('Sites'), blank=True,
null=True,
help_text=_('Select sites in which show the project.'))
article_url = models.CharField(_('Article Url'), max_length=310, blank=False,
help_text=_('Use to link to the original source'))
position = models.PositiveIntegerField(default=0, blank=False, null=False)
....
管理员是一个标准的管理员:
class ArticleAdmin(MultisiteAdminMixin,
VersionedPlaceholderAdminMixin,
FrontendEditableAdminMixin,
SortableAdminMixin,
TranslatableAdmin):
def sites_visible_on(self, obj):
return ', ' .join('%s' % site.name for site in obj.sites.all())
def countries_visible_on(self, obj):
qs = obj.countries_displayed_in.all()
if not len(qs):
return _('All')
return ', ' .join('%s' % country.name
for country in obj.countries_displayed_in.all())
sites_visible_on.short_description = _('Sites visible on')
countries_visible_on.short_description = _('Countries visible from')
list_display = [
'__str__', 'date_realization',
'sites_visible_on', 'countries_visible_on',
'language_column',
'featureArticle'
]
search_fields = ('translations__title',)
date_hierarchy = 'date_realization'
frontend_editable_fields = ('title', 'source', 'description')
list_per_page = 10
fieldsets = [
('Archive article', {
'fields': [('title', 'sources'), ('slug', 'date_realization'), ('regions', 'global_regions'), 'article_url']
}),
('Feature', {
'fields': ('featureArticle', 'image', 'description'),
}),
('Localization', {
'fields': ('sites', 'countries_displayed_in'),
'classes': ('collapse',)
}),
('SEO', {
'fields': [('meta_description', 'meta_title', 'meta_keywords')],
'classes': ('collapse',)
}),
]
def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('title',)}
class ArticleSourceAdmin(VersionedPlaceholderAdminMixin,
FrontendEditableAdminMixin,
SortableAdminMixin,
TranslatableAdmin):
list_display = ['__str__', 'language_column']
def get_prepopulated_fields(self, request, obj=None):
return {'slug': ('name',)}
admin.site.register(ArticleSource,ArticleSourceAdmin)
管理员.站点.注册(文章,文章管理员)
因此,除2种情况外,所有情况都显示正确:
migrations.CreateModel(
name='ArticleSource',
fields=[
('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)),
('date_created', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
('date_modified', models.DateTimeField(auto_now=True, verbose_name='modified at')),
('position', models.PositiveIntegerField(default=0)),
],
options={
'verbose_name_plural': 'Press sources',
'ordering': ('position',),
'verbose_name': 'Press source',
},
bases=(models.Model,),
),
嗯,我完全迷路了,它在展示室应用程序中工作得很好,但这次显然不行。今天是迁移的灾难之日-我刚刚回答了一个问题 因此,在您的情况下,Django不知何故认为它已经创建了表,但实际上它没有。要重新运行迁移,您应该通过执行以下命令使Django认为它尚未运行任何迁移:
manage.py migrate yourapp zero --fake
然后逐个应用迁移以跟踪任何错误:
manage.py migrate yourapp 0001
manage.py migrate yourapp 0002
诸如此类。不过要小心,因为您很可能会丢失数据。备份总是起作用的。今天是迁移的灾难之日-我刚刚回答了一个问题 因此,在您的情况下,Django不知何故认为它已经创建了表,但实际上它没有。要重新运行迁移,您应该通过执行以下命令使Django认为它尚未运行任何迁移:
manage.py migrate yourapp zero --fake
然后逐个应用迁移以跟踪任何错误:
manage.py migrate yourapp 0001
manage.py migrate yourapp 0002
等等。但是要小心,因为你很可能会丢失数据。备份总是起作用的。如果你运行
manage.py migrate your app zero
,然后重新运行manage.py migrate your_app
-它是否打印出它已经创建了ArticleSource
模型?事实上,它只写:要执行的操作:A应用所有迁移:djangcms_按Ok,然后再次转到zero
并逐个尝试迁移,例如manage.py迁移你的应用程序0001
等等。好的,顺便说一句,当我执行zero时,我有一个错误弹出:psycopg2.ProgrammingError:table“djangcms_按文章来源”“操作后不存在:要执行的操作:特定于目标的迁移:0001_initial,从djangocms_按Running migrations:No migrations to apply。如果您运行manage.py migrate your app zero
,然后重新运行manage.py migrate your_app
-它是否打印出它已经创建了ArticleSource
模型?实际上,它只写:要执行的操作:apply all migrations:djangocms_按OK,然后再次转到zero
并逐个尝试迁移,例如manage.py迁移您的应用程序0001
等等。好的,顺便说一句,当我执行zero时,我出现了错误poping:psycopg2.ProgrammingError:table“djangocms\u press\u article\u sources”操作后不存在所以:要执行的操作:特定于目标的迁移:0001_initial,从djangocms_按Running migrations:No migrations to apply。