在 Django 中添加 ManyToManyField 时出错
好的,所以我最近发布了一个关于添加 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
同样的事情,我回答你的另一个问题这是我无法添加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
这或多或少是我会做的方式。事实上,我很困惑为什么这不起作用,并在干净的 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?