0

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']
HereHere
  • 734
  • 1
  • 7
  • 24
  • So, if there is a note about "Django doesn’t currently provide any support for foreign key spanning multiple databases", what are you trying to do? – coldmind Jun 15 '15 at 16:18
  • I don't think that even SQLite supports cross-database foreign keys. You're best bet is to simulate it with an integer column that contains the primary key of whatever you're referencing, possibly creating a custom type to hide this from the code that uses it. – Colonel Thirty Two Jun 15 '15 at 17:56

0 Answers0