Home > OS >  django database path : no unsupported opperand type
django database path : no unsupported opperand type

Time:05-29

In the django settings.py, the database is by default :

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

But when i do in python a={'a':'a'/'b'}, i get the error TypeError: unsupported operand type(s) for /: 'str' and 'str'. How come the error doesnt show up in django?

I would like to define a different path for my database, in a subfolder so as django automatically create the subfolder and the sqlite database...how can i do that?

thank you

CodePudding user response:

But when i do in python a={'a':'a'/'b'}, I get the error TypeError: unsupported operand type(s) for /: 'str' and 'str'. How come the error doesnt show up in django?

Because BASE_DIR is not a string, but a Path object [Python-doc]. For this object, the __div__ method has been defined, and it thus uses some_path / 'foo.sqlite3' to join the path at the left side with the right string. Indeed, for example:

>>> Path('/etc') / 'foo' / 'bar.data'
PosixPath('/etc/foo/bar.data')

I would like to define a different path for my database, in a subfolder so as django automatically create the subfolder and the sqlite database...how can i do that?

You can specify a different path, for example with:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'subdir' / 'db.sqlite3',
    }
}
  • Related