将第二个旧数据库添加到现有 django 1.2 项目
我有一个 django 项目,已经使用了一段时间,但现在我想从第二个非 django 数据库添加一些新数据集。我已成功配置我的 settings.py 文件以使用新数据库,该数据库称为“mediawikidb”。
我可以运行以下命令:
python manage.py inspectdb --database=mediawikidb
并且我获得了数据库的 django 模型表示,但我感兴趣的表称为“用户”。 Inspectdb 的输出如下所示:
class User(models.Model):
user_id = models.IntegerField(primary_key=True)
user_name = models.CharField(unique=True, max_length=255)
user_real_name = models.CharField(max_length=255)
user_password = models.TextField()
user_newpassword = models.TextField()
user_newpass_time = models.CharField(max_length=14, blank=True)
user_email = models.TextField()
user_options = models.TextField()
user_touched = models.CharField(max_length=14)
user_token = models.CharField(max_length=32)
user_email_authenticated = models.CharField(max_length=14, blank=True)
user_email_token = models.CharField(max_length=32, blank=True)
user_email_token_expires = models.CharField(max_length=14, blank=True)
user_registration = models.CharField(max_length=14, blank=True)
user_editcount = models.IntegerField(null=True, blank=True)
class Meta:
db_table = u'user'
我一直在阅读文档 [0] 但我很困惑,所以我想在阅读了一段时间后我会在这里问。我不能把它放到我的模型中,可以吗?如果我这样做肯定会导致 django 出现问题。我该怎么做?
我的另一个问题是,django 如何知道这个“用户”模型实例与哪个数据库相关?我觉得我错过了一些东西,但我找不到任何有意义的东西。
我应该如何设置我的模型才能访问这些数据而不弄乱 django 中的任何内容?
I have a django project that I have been using for a while, but now I want to add some a new dataset from a second, non-django database. I have successfully configured my settings.py file to use the new database, which is called 'mediawikidb'.
I am able to run the following command:
python manage.py inspectdb --database=mediawikidb
and I get django's model representations for the database, but the table that I am interested in is called 'User'. The output of inspectdb looks like this:
class User(models.Model):
user_id = models.IntegerField(primary_key=True)
user_name = models.CharField(unique=True, max_length=255)
user_real_name = models.CharField(max_length=255)
user_password = models.TextField()
user_newpassword = models.TextField()
user_newpass_time = models.CharField(max_length=14, blank=True)
user_email = models.TextField()
user_options = models.TextField()
user_touched = models.CharField(max_length=14)
user_token = models.CharField(max_length=32)
user_email_authenticated = models.CharField(max_length=14, blank=True)
user_email_token = models.CharField(max_length=32, blank=True)
user_email_token_expires = models.CharField(max_length=14, blank=True)
user_registration = models.CharField(max_length=14, blank=True)
user_editcount = models.IntegerField(null=True, blank=True)
class Meta:
db_table = u'user'
I have been reading the docs [0] but I am pretty confused so I thought I would ask here after reading around for a while. I can't just drop this in my model, can I? If I do it will cause issues with django for sure. How should I do this?
My other question is, how will django know which database this 'User' model instance relates to? I feel like I am missing something, but I can't find anything that makes sense.
How should I set up my model to be able to access this data without messing up anything in django?
[0] http://docs.djangoproject.com/en/dev/topics/db/multi-db/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想您担心表被称为
user
,模型类被称为User
。 Django 使用app_modelname
作为其表,因此django.contrib.auth
的表是auth_user
Django 不知道您的 User 模型与哪个数据库相关到。它将采用默认值,因此它将查找您的数据库中可能不存在的表
user
。要查询它,您需要指定特定数据库 (
User.objects.using('mydb').all()
) 或按照 Dimitry 建议设置自动数据库路由。I presume you are worried about the table being called
user
, and the model class being calledUser
. Django usesapp_modelname
for its tables, sodjango.contrib.auth
's table isauth_user
Django doesn't know which database your User model relates to. It will assume the default, so it will look for a table
user
which probably doesn't exist in your db.To query it, you will need to specify a specific database (
User.objects.using('mydb').all()
) or set up automatic database routing as Dimitry suggested.检查以下文档:
http://docs.djangoproject .com/en/dev/topics/db/multi-db/#an-example
他们有一个路由器示例,指示从何处读取数据和向何处写入数据。对于您的用户模型,您可以配置与 mediawiki 数据库的所有交互,所有其他模型将使用您的普通数据库。
只要确保禁用关系...
Check the following docs:
http://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example
They have an example of a router that indicates where to read data from and write data to. For your user model, you can configure all interactions with the mediawiki database and all other models would use your normal database.
Just make sure to disable relations...