在 Django 中添加 ManyToManyField 时出错

发布于 2024-08-18 18:49:18 字数 905 浏览 4 评论 0原文

好的,所以我最近发布了一个关于添加 ManyToManyField 时出现错误的问题。

模型如下所示

class MagicType(models.Model):

     name = models.CharField(max_length=155)
     parent = models.ForeignKey('self', null=True, blank=True)

class Spell(models.Model):

    name = models.CharField(max_length=250, db_index=True)
    magic_words = models.CharField(max_length=250, db_index=True)
    magic_types = models.ManyToManyField(MagicType)

这是我使用 django-evolution 迁移时遇到的错误:

AttributeError: 'ManyToManyField' object has no attribute '_get_m2m_column_name'

所以,有没有办法手动设置 ManyToManyField 而不在两个模型中指定它?假设有一个这样的模型

class SpellToMagicType(models.Model):
     magic_type = models.ForeignKey(MagicType)
     spell = models.ForeignKey(Spell)

,但是我将如何在 Django 的 ORM 中继续使用它呢?

非常感谢您的帮助。谢谢!

Ok, so I posted a question recently regarding an error when adding a ManyToManyField

The Models are the ones below

class MagicType(models.Model):

     name = models.CharField(max_length=155)
     parent = models.ForeignKey('self', null=True, blank=True)

class Spell(models.Model):

    name = models.CharField(max_length=250, db_index=True)
    magic_words = models.CharField(max_length=250, db_index=True)
    magic_types = models.ManyToManyField(MagicType)

This is the error I get when migrating with django-evolution:

AttributeError: 'ManyToManyField' object has no attribute '_get_m2m_column_name'

So, is there a way of manually setting a ManyToManyField without specifying it within the two models? let's say with a model like this

class SpellToMagicType(models.Model):
     magic_type = models.ForeignKey(MagicType)
     spell = models.ForeignKey(Spell)

but how would I go on about using this within Django's ORM?

Help would be very much appreciated. Thanks!

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

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

发布评论

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

评论(2

零時差 2024-08-25 18:49:18

同样的事情,我回答你的另一个问题这是我无法添加ManyToManyField的原因吗?基本上这个错误是因为你在模型中的代码(ORM) 更改,但您的数据库没有更改,并且 django-evolution 不能解决数据库中更改的许多问题,我建议您寻找 django-extensions (http://code.google.com/p/django-command-extensions/) 和命令 sqldiff,但看看我的另一个答案

Same thing, i answer your another question Is a reason I can't add a ManyToManyField? basicly this error is because your code in models (ORM) change but your database isn't, and django-evolution doesn't fix many problems with changes in the database, i suggest you look for django-extensions (http://code.google.com/p/django-command-extensions/) and the command sqldiff, but look my another answer

如果没结果 2024-08-25 18:49:18

这或多或少是我会做的方式。事实上,我很困惑为什么这不起作用,并在干净的 Django 1.1 环境中亲自尝试过——它运行得很好。

您是否尝试过亲自将这个模型放入干净的环境中并看看会得到什么?

This is more or less the way I would have done it. In fact, I was puzzled about why this didn't work, and tried it myself in a clean Django 1.1 environment -- it works swimmingly.

Have you tried putting this model in a clean environment yourself and seeing what you get?

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