将第二个旧数据库添加到现有 django 1.2 项目

发布于 2024-10-21 01:12:47 字数 1505 浏览 2 评论 0原文

我有一个 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 中的任何内容?

[0] http://docs.djangoproject.com/en/dev /主题/db/多数据库/

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 技术交流群。

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

发布评论

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

评论(2

来日方长 2024-10-28 01:12:47

我想您担心表被称为 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 called User. Django uses app_modelname for its tables, so django.contrib.auth's table is auth_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.

满天都是小星星 2024-10-28 01:12:47

检查以下文档:

http://docs.djangoproject .com/en/dev/topics/db/multi-db/#an-example

他们有一个路由器示例,指示从何处读取数据和向何处写入数据。对于您的用户模型,您可以配置与 mediawiki 数据库的所有交互,所有其他模型将使用您的普通数据库。

只要确保禁用关系...

class MyAppRouter(object):

    def _is_user_model(model):
        return str(model.__name__) == 'User' and model._meta.app_label == 'myapp'

    def db_for_read(self, model, **hints):
        if self._is_user_model(model):
            return 'mediawikidb'
        return None

    def db_for_write(self, model, **hints):
        if self._is_user_model(model):
            return 'mediawikidb'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if self._is_user_model(obj1) or self._is_user_model(obj2):
            return False
        return None

    def allow_syncdb(self, db, model):
        if db == 'mediawikidb':
            return False
        elif self._is_user_model(model):
            return False
        return None

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...

class MyAppRouter(object):

    def _is_user_model(model):
        return str(model.__name__) == 'User' and model._meta.app_label == 'myapp'

    def db_for_read(self, model, **hints):
        if self._is_user_model(model):
            return 'mediawikidb'
        return None

    def db_for_write(self, model, **hints):
        if self._is_user_model(model):
            return 'mediawikidb'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        if self._is_user_model(obj1) or self._is_user_model(obj2):
            return False
        return None

    def allow_syncdb(self, db, model):
        if db == 'mediawikidb':
            return False
        elif self._is_user_model(model):
            return False
        return None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文