django-database

Django: dynamic database file

你说的曾经没有我的故事 提交于 2019-11-27 11:58:53
In my Django project I have a dependency for a third party application that produces SQLite cache files in various directories with a known schema. I'd like to use Django models to access those databases, but obviously I cannot use a static DATABASES setup. How can I dynamically open a SQLite database on an arbitrary path? EDIT As Byron Ruth pointed out, the solution is to use the django.db.connections in conjunction with the using function in the QuerySet. Byron Ruth The django.db.connections is a simple wrapper around DATABASES defined in your settings. The wrapper class is here: django.db

Django - Rollback save with transaction atomic

人走茶凉 提交于 2019-11-27 01:16:21
问题 I am trying to create a view where I save an object but I'd like to undo that save if some exception is raised. This is what I tried: class MyView(View): @transation.atomic def post(self, request, *args, **kwargs): try: some_object = SomeModel(...) some_object.save() if something: raise exception.NotAcceptable() # When the workflow comes into this condition, I think the previous save should be undome # Whant am I missing? except exception.NotAcceptable, e: # do something What am I doing wrong

dynamically set database based on request in django

半腔热情 提交于 2019-11-27 00:39:38
问题 I am writing a multi-tenant application with python-django. I want to set database connection based on each request.I thought i could write a middleware where we set the database to be used for that particular database. import re from django.db import connections class SetTenantDatabase(object): def process_request(self, request): pattern = re.compile("\\b(http://|https://|www.|.com|8000|:|//)\\W\\d+", re.I) words = request.get_host() db_name = [pattern.sub("", words)][0].split('.')[0]

Django - how to specify a database for a model?

无人久伴 提交于 2019-11-26 23:00:52
问题 Is there a way to specify that a model (or app, even) should only ever use one particular database? I am working with a legacy database that I don't want to change. I have two databases - the 'default' is an sqlite one that could be used for admin etc, and the legacy one. I used inspectdb to create a model for (part of) the legacy database, and it has managed = False . But is there a way to specify in the model itself that it only applies to a particular database? I see that you can specify

Django multiple and dynamic databases

房东的猫 提交于 2019-11-26 10:23:37
问题 I\'ve been evaluating django and wondered if the following is possible. I\'ve already looked at the regular multiple database docs so please don\'t point me to that because this use case isn\'t mentioned as far as i can make out. If i\'m wrong i take it back :) I want one main database in which most of my app\'s models will reside, however one of the app\'s will need to dynamically create databases, these will be customer specific databases. The database path (i plan to use sqlite) will be