How to do two databases relationship ( OneToOneField )

523 views Asked by At

if is not supported in Django?

I read from Django documentaion:

Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases.

So I have two databases DB_1 and DB_2

DB_1: All inserts and updates will go to the default database( DB_1 ).

DB_2 (read_only): All read queries will go to DB_2 database

I have this class:

class Task(models.Model):
    person = models.OneToOneField(Person)
    start_date = models.DateField(null=True)
    end_date = models.DateField(null=True)
    free_day = models.DateField(null=True)
    text = models.CharField(max_length=100)

    def __str__(self):
        return '%s' % self.person

and I'm trying to create object by using shell

p = Person.objects.get(username='Herehere')
t = Task(person=p,text='blabla')
t.save()

But I got this error:

AttributeError: 'DatabaseWrapper' object has no attribute 'Database'

If I change

person = models.OneToOneField(Person)

to

person = models.OneToOneField(Person, primary_key)

I got

'DatabaseWrapper' object has no attribute 'operators'

My settings.py db configurations:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'ldap': {
        'ENGINE': 'ldapdb.backends.ldap',
        'NAME': AUTH_LDAP_SERVER_URI,
        'USER': AUTH_LDAP_BIND_DN,
        'PASSWORD': AUTH_LDAP_BIND_PASSWORD,
    },
}

DATABASE_ROUTERS = ['ldapdb.router.Router']
0

There are 0 answers