使用自定义字段自定义南迁移 - Django

发布于 2024-11-30 00:41:49 字数 2172 浏览 2 评论 0原文

我对 Django 还很陌生,刚刚找到了一份工作,涉及维护和添加功能到我没有设计的网站,所以我仍然对项目的结构和不是什么感到困惑。该站点使用 South 进行数据库迁移,我已经掌握了使用它向项目添加新应用程序的窍门。我现在遇到的麻烦是我需要删除模型中的某个字段,因为不再需要它,并且在管理页面上需要填写它。到目前为止,根据我对 Django 的理解,它似乎是一个自定义字段。它在自己单独的库应用程序中是这样定义的(仍然不确定这是否是正确的行话)。

class Genre(models.Model):
    name = models.CharField(max_length=255)
    def __unicode__(self):
        return u"%s" % self.name

这是使用自定义字段的模型(如果有帮助的话)。

class Entry(models.Model):
    artist = d51fields.ForeignKey(Artist, instantiate_fn=instant_artist)
    album = d51fields.ForeignKey(Album, js_methods=['match_artist_and_startswith'], instantiate_fn=instant_album)
    track = d51fields.ForeignKey(Track, js_methods=['match_album_and_startswith'], instantiate_fn=instant_track)
    genre = models.ForeignKey(Genre)
    submitted = models.DateTimeField(auto_now_add=True)
    is_rotation = models.BooleanField()
    dj = models.ForeignKey(DJ)
    show = models.ForeignKey(Show, null=True, blank=True)
    objects = EntryManager()
    def __unicode__(self):
        return "%s [%s]" % (self.artist, self.track)
    class Meta:
        verbose_name = "entry"
        verbose_name_plural = "entries"

我已经查看了迁移自定义字段的文档,但这对我来说真的很困惑,所以我正在寻求更多帮助。我只想删除保存 Genre 字段的表,并清理与其关联的外键的依赖关系。您认为我应该为 South 编写一些自定义规则并使用迁移,还是尝试在 Postgresql 中手动执行此操作。我尝试只用 Postgres 来做这件事,但失败得很惨。

任何指导将不胜感激。如果您想了解有关情况的更多信息,请询问,我可以将其添加到帖子中。我有一种感觉,我必须处理很多依赖项,但希望有一个简单的修复。

另外,如果有人知道如何很好地了解数据库结构,那就太好了。

非常感谢。你们都很棒。

Edit1

这是我删除外键然后运行时得到的结果,

manage.py schemamigration logs --auto

 ! Cannot freeze field 'logs.entry.artist'
 ! (this field has class d51_admin_autofk.fields.ForeignKey)
 ! Cannot freeze field 'logs.entry.album'
 ! (this field has class d51_admin_autofk.fields.ForeignKey)
 ! Cannot freeze field 'logs.entry.track'
 ! (this field has class d51_admin_autofk.fields.ForeignKey)

 ! South cannot introspect some fields; this is probably because they are custom
 ! fields. If they worked in 0.6 or below, this is because we have removed the
 ! models parser (it often broke things).

我不完全确定下一步应该采取什么样的操作。我查看了南方文档,并不太清楚如何编写迁移此类内容的规则。

I am pretty new to Django and just got a job that involves maintaining and adding features to a site I did not design, so I am still kind of confused about the structure and what not of the project. The site is using South for database migrations and I've got a hang of using it to add new applications to the project. The trouble I am having now is that I need to delete a certain field in a model because it is no longer needed and on the admin page it is required to be filled out. From my understanding of Django so far it appears to be a custom field. It is defined like this in its own separate library application(still not sure if thats the right lingo).

class Genre(models.Model):
    name = models.CharField(max_length=255)
    def __unicode__(self):
        return u"%s" % self.name

Here is the models that uses the custom field if that helps out any.

class Entry(models.Model):
    artist = d51fields.ForeignKey(Artist, instantiate_fn=instant_artist)
    album = d51fields.ForeignKey(Album, js_methods=['match_artist_and_startswith'], instantiate_fn=instant_album)
    track = d51fields.ForeignKey(Track, js_methods=['match_album_and_startswith'], instantiate_fn=instant_track)
    genre = models.ForeignKey(Genre)
    submitted = models.DateTimeField(auto_now_add=True)
    is_rotation = models.BooleanField()
    dj = models.ForeignKey(DJ)
    show = models.ForeignKey(Show, null=True, blank=True)
    objects = EntryManager()
    def __unicode__(self):
        return "%s [%s]" % (self.artist, self.track)
    class Meta:
        verbose_name = "entry"
        verbose_name_plural = "entries"

I've looked at the documentation for migrating custom fields but it is all really confusing for me, so I am looking for some more help. I just want to get rid of the table holding the Genre field and clean up the dependencies with the foreign keys associated with it. Do you think I should write some custom rules for South and use a migration or just try and do it manually in Postgresql. I tried doing it with just Postgres and I failed miserably.

Any guidance would be greatly appreciated. If you want more info about the situation just ask and I can add it to the post. I have a feeling there is a lot of dependencies I will have to deal with, but hopefully there is a simple fix.

Also if some one knows how to get a good view of the database structure that would be great.

Thanks so much. All of you guys are great.

Edit1

Here what I got when I removed the ForeignKeys and then ran

manage.py schemamigration logs --auto

 ! Cannot freeze field 'logs.entry.artist'
 ! (this field has class d51_admin_autofk.fields.ForeignKey)
 ! Cannot freeze field 'logs.entry.album'
 ! (this field has class d51_admin_autofk.fields.ForeignKey)
 ! Cannot freeze field 'logs.entry.track'
 ! (this field has class d51_admin_autofk.fields.ForeignKey)

 ! South cannot introspect some fields; this is probably because they are custom
 ! fields. If they worked in 0.6 or below, this is because we have removed the
 ! models parser (it often broke things).

I am not totally sure what sort of action I should take next. I looked into the South documentation and it wasn't too clear about how to write the rules for migrating things like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-12-07 00:41:49

我在您发布的代码中没有看到任何自定义字段。我看到的只是两个模型,都包含 Django 附带的标准字段。

如果我理解正确的话,您只需删除对 Genre 模型的所有 ForeignKey 引用即可。然后运行 ​​./manage.py schemamigration; --自动。这将要求您为 Entry 模型中的 genre 字段提供默认值,并提供某种 ID。 (这是因为迁移可以向前和向后应用,因此如果您尝试撤消迁移,这是将插入到所有模型实例中的值。)

最后,只需应用迁移就可以实现:./manage.py 迁移

之后,您应该可以安全地删除存储您的 Genre 模型的表。

不过,请务必在开发服务器上尝试此操作,以确保它不会崩溃。 (-;

I don't see any custom field anywhere in the code you posted. All I see is two models, all containing standard fields shipped with Django.

If I understand correctly, you can just delete all ForeignKey references to your Genre model. Then run ./manage.py schemamigration <yourappname> --auto. This will ask you for a default value for the genre field in the Entry model, provide an ID of some kind. (This is because migrations can be applied both forwards and backwards, so if you try to undo the migration, this is the value that will get inserted in all your model instances.)

Finally, just applying the migration should make it happen: ./manage.py migrate <yourappname>.

After that you should be safe to drop the table storing your Genre model.

Be sure to try this on a development server though, just to make sure it doesn't blow up. (-;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文