django-database

Configure Django Database Routers

懵懂的女人 提交于 2019-12-11 14:45:10
问题 I am trying to connect a new Django site to legacy DBs. From everything I can gather I need to create a database router. The docs refer to creating an app_label in the meta section of the model. This is what the router would match to. I have also seen where people say this is not longer supported. I am racking my brain here please help!! Here is the code: class CucRouter(object): def db_for_read(self, model): if model._meta.app_label == 'CUCMCDR': return 'CUCMCDR' return 'default' That is the

Alternative for django.db.close_connection()

寵の児 提交于 2019-12-11 03:38:50
问题 Is there a possible way to re-establish MySQL connection in Django after it goes down? I am getting the following error while trying to access MySQL using the get_wsgi_application in django.core.wsgi : (2006,'MYSQL server has gone away') 回答1: We need to close the old connection to db, which Django had automatically created before. We can do this by executing the function: django.db.close_old_connections() Django automatically creates new connections after closing previous connections. 来源:

Accidentally deleted my django south migration directory

筅森魡賤 提交于 2019-12-11 03:15:20
问题 I accidentally deleted the South migrations directory for one of my Django apps. This directory was not under git. So now there are migrations in the database that are not present on the disk. Some pointers on how I can recover from this will be much appreciated. 回答1: Without the option of any type of backup or finding the files somewhere, what you're going to have to do is make initial migrations and then fake them. $ ./manage.py schemamigration app --initial $ ./manage.py migrate app --fake

Django modeltranslation - can't get and see original fields

断了今生、忘了曾经 提交于 2019-12-08 11:29:36
问题 I'm trying to use django-modeltranslation in my project. For now, just for Tag model with one field - name . I've created and registered TranslationOptions , then makemigrations and migrate . Now I can't access the original name text. It seems to be replaced with '' (empty string) but it isn't: In [6]: Tag.objects.first() Out[6]: <Tag: > In [7]: Tag.objects.first().name Out[7]: u'' In [8]: Tag.objects.first().__dict__ Out[8]: {'_state': <django.db.models.base.ModelState at 0x7fc96ad41710>,

django.db.utils.IntegrityError: (1062, “Duplicate entry '22-add_' for key 'content_type_id'”)

强颜欢笑 提交于 2019-12-08 01:25:27
I am using django multiple DB router concepts, having multiple sites with different db's. Base database user will login with all other sub sites. When i try syncdb in base site its worked properly(at any time), but trying syncdb with other sites works first time only, if we try next time on-wards it throws integiry error like below django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_somesame' for key 'content_type_id'") Once i removed multiple DB router settings in that project means syncdb works properly(at any time). So is this relates to multiple db router? or what else? Please

Django: how to wrap a bulk update/insert operation in transaction?

為{幸葍}努か 提交于 2019-12-07 23:38:20
问题 This is my use case: I have multiple celery tasks that run in parallel Each task could Bulk create or update many objects. For this I'm using django-bulk So basically I'm using a very convenient function insert_or_update_many: it first performs a Select if it finds objects it updates them Otherwise it creates them But this introduces problems of concurrency. For example: if an object did not exist during the step 1 then it is added to a list of objects to be inserted later. But during this

Django : Call a method only once when the django starts up

杀马特。学长 韩版系。学妹 提交于 2019-12-07 05:04:22
问题 I want to initialize some variables (from the database) when Django starts. I am able to get the data from the database but the problem is how should I call the initialize method . And this should be only called once. Tried looking in other Pages, but couldn't find an answer to it. The code currently looks something like this :: def get_latest_dbx(request, ....): #get the data from database def get_latest_x(request): get_latest_dbx(request,x,...) def startup(request): get_latest_x(request)

Models does not create tables when synched

假如想象 提交于 2019-12-06 10:26:23
问题 I have some django models for my extended users profile. Problem is that this code does not create tables when syncdb is used (simply nothing happens. No validation errors). Why is that happening? (Also those models give import error elsewhere) : #!/usr/bin/env python # encoding: utf-8 from django.db import models from django.contrib.auth.models import User from registration.signals import user_registered from forms import ExtendedRegistrationForm import hashlib class InheritedProfile(models

Django: how to wrap a bulk update/insert operation in transaction?

倖福魔咒の 提交于 2019-12-06 05:54:10
This is my use case: I have multiple celery tasks that run in parallel Each task could Bulk create or update many objects. For this I'm using django-bulk So basically I'm using a very convenient function insert_or_update_many : it first performs a Select if it finds objects it updates them Otherwise it creates them But this introduces problems of concurrency. For example: if an object did not exist during the step 1 then it is added to a list of objects to be inserted later. But during this period can happen that another Celery task has created that object and when it tries to perform a bulk

Django : Call a method only once when the django starts up

╄→гoц情女王★ 提交于 2019-12-05 07:54:03
I want to initialize some variables (from the database) when Django starts. I am able to get the data from the database but the problem is how should I call the initialize method . And this should be only called once. Tried looking in other Pages, but couldn't find an answer to it. The code currently looks something like this :: def get_latest_dbx(request, ....): #get the data from database def get_latest_x(request): get_latest_dbx(request,x,...) def startup(request): get_latest_x(request) Hui Zheng Some people suggest( Execute code when Django starts ONCE only? ) call that initialization in