django将ManyToMany字段/表添加到现有模式,related_name错误 [英] django adding a ManyToMany field/table to existing schema, related_name error

查看:695
本文介绍了django将ManyToMany字段/表添加到现有模式,related_name错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的项目与模型(用户和书籍)。我想在现有的模型图书中添加一个ManyToMany(M2M)字段,但是syncbb命令不会这样做。



详细信息:图书已经有一个FK字段映射到用户,我想添加一个也映射到用户的新的M2M字段(读者)。如你所知,Django的syncdb只关心表,所以添加一个正常的字段很容易,但是M2M需要一个新的连接表(app_books_user),所以不应该像syncdb cmd那样添加任何其他的新表吗?它创建了我的另一个加入的表的书的'卖家'字段。



当我运行syncdb,我最初收到一个错误,指示我使用'related_name'参数来帮助区分两个对User的引用。我加了那些但是,当我再次运行syncdb时,它不会创建新的连接表(但是它现在没有错误)。当我通过Shell查看它时,新的字段存在,但不能使用它b / c连接表不存在。我通过'sqlall'cmd查看了sql代码,并打印出新表的SQL,但没有执行。



我缺少什么?我应该通过我的数据库浏览器强制SQL(从sqlall)?会有什么影响吗?代码如下:



Models.py

  from django.contrib。 auth.models import User 

class卖家(models.Model):
...

类图书(models.Model):

name = models.CharField(max_length = 50)
author = models.ForeignKey(User,related_name ='creator')
reader = models.ManyToManyField(User,blank = True,related_name =' ')
sellers = models.ManyToManyField(卖方)

谢谢

解决方案

syncdb不修改现有表(型号)。发生的是即使添加M2M也不会修改表(它应该只是添加一个连接表),所以syncdb不会创建新的表,因为它看到模型已经在数据库中,并且正在跳过它。请参阅 syncdb docs


[syncdb]为INSTALLED_APPS中尚未创建其表的所有应用程序创建数据库表。


这是一个有点混乱,因为从技术上来说,这个表没有被创建,但行为是有道理的有这个模型的表已创建吗?是的,然后不要同步它。 p>

通常,django不提供迁移数据库模式的机制(即添加列等)。因此,您必须通过原始SQL或使用第三方工具来执行此类操作。我强烈建议您查看南方


I have an existing project with models (Users and Books). I would like to add a ManyToMany(M2M) field to the existing model Books, but the syncbb command does not do this.

Details: Books already has a FK field that maps to User, and I want to add a new M2M field (readers) that also maps to User. As you know, Django's syncdb only cares about tables, so adding a normal field is easy, but a M2M requires a new join table (app_books_user), so shouldn't syncdb cmd add this like any other new table? It created my other joined table for the Book's 'sellers' field.

When I ran syncdb, I initially received an error instructing me to use the 'related_name' argument to help differentiate the two references to User. I added those. However, when I run syncdb again, it does not create the new join table (but it's now error-free). The new field exists when I view it via the Shell, but can't use it b/c the join table doesn't exist. I looked at the sql code via 'sqlall' cmd and it prints out the SQL for the new table, but it doesn't get executed.

What am I missing? Should I just force the SQL (from the sqlall) via my database browser? Will that have any repercussions? Code follows:

Models.py

from django.contrib.auth.models import User

class Seller(models.Model):
    ...

class Books(models.Model):

    name=models.CharField(max_length=50)
    author=models.ForeignKey(User, related_name='creator')
    readers=models.ManyToManyField(User, blank=True, related_name='viewers')
    sellers=models.ManyToManyField(Seller)

Thank you

解决方案

syncdb does not modify existing tables (models). What's going on is that even though adding a M2M does not modify a table (it should just add a join table), the syncdb is not creating the new table because it sees that the model is already in the db and it is skipping it. See the syncdb docs

[syncdb] Creates the database tables for all apps in INSTALLED_APPS whose tables have not already been created.

It is a bit confusing, since technically this table hasn't been created, but the behavior makes sense "Has the table for this model been created? Yep! Then don't sync it."

In general, django does not provide a mechanism to migrate database schemas (ie add columns and such). So, you have to do that sort of a thing via raw SQL or by using a third party tool. I highly recommend checking out South.

这篇关于django将ManyToMany字段/表添加到现有模式,related_name错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆