django-postgresql

Specifying Readonly access for Django.db connection object

妖精的绣舞 提交于 2020-01-03 17:23:47
问题 I have a series of integration-level tests that are being run as a management command in my Django project. These tests are verifying the integrity of a large amount of weather data ingested from external sources into my database. Because I have such a large amount of data, I really have to test against my production database for the tests to be meaningful. What I'm trying to figure out is how I can define a read-only database connection that is specific to that command or connection object.

Specifying Readonly access for Django.db connection object

橙三吉。 提交于 2020-01-03 17:23:21
问题 I have a series of integration-level tests that are being run as a management command in my Django project. These tests are verifying the integrity of a large amount of weather data ingested from external sources into my database. Because I have such a large amount of data, I really have to test against my production database for the tests to be meaningful. What I'm trying to figure out is how I can define a read-only database connection that is specific to that command or connection object.

django database routing with transactions

亡梦爱人 提交于 2019-12-23 09:31:45
问题 Referring to the example in Django documentation for multiple databases in one application, https://docs.djangoproject.com/en/dev/topics/db/multi-db/#an-example " It also doesn’t consider the interaction of transactions with the database utilization strategy. " How do I handle the interaction stated above. The scenario is this: I am using postgresql as my database. I have setup up a replica and want all the reads to "auth" tables to go to replica. Following the documentation I wrote a

Django Admin Model ArrayField Change Delimiter

感情迁移 提交于 2019-12-23 05:26:33
问题 My model looks like this: from django.contrib.postgres.fields import ArrayField class Trigger(models.Model): solutions = ArrayField(models.TextField(blank=True, null=True), blank=True, null=True, help_text='some helpful text') This allows me to enter a list of solutions separated by a comma by default. For example: I can enter in that textfield: 1. watch out for dust., 2. keep away from furry animals., This does create a list of two separate string items. However, if a solution text itself

Django Query extremely slow

只愿长相守 提交于 2019-12-22 06:44:53
问题 i got a problem with an Django application. Queries on the model Scope are extremly slow and after some debugging i still got no clue where the problem is. When i query the db like scope = Scope.objects.get(pk='Esoterik I') in django it takes 5 to 10 seconds. The database has less than 10 entries and an index on the primary key. so it is way too slow. When executing the an equivalent query on the db like SELECT * FROM scope WHERE title='Esoterik I'; everything is ok, it takes only about 50ms.

Postgres: values query on json key with django

折月煮酒 提交于 2019-12-18 21:12:07
问题 I need to do a values/values_list query on nested key on a postgres backed jsonfield in django 1.10 eg. class AbcModel(models.model): context = fields.JSONField() If it has values like: { 'lev1': { 'lev': 2 } } I want to run a queries like AbcModel.objects.values('context__lev1__lev2').distinct() AbcModel.objects.values_list('context__lev1__lev2', flat=True).distinct() EDIT: The JSON fields are the official django JSONField from django.contrib.postgres.fields 回答1: So I found a solution, this

Does a reverse sql inspector exist for django & postgres

北战南征 提交于 2019-12-13 05:24:23
问题 Does a reverse sql inspector exist for django & postgres? Something that shows you what is the origin of the sql view in django? Something like this: https://github.com/rory/django-sql-inspector, but for postgres. (Please let me know if you don't know of one, because hearing a handful of respectable peeps say they haven't is better than silence.) 回答1: I can suggest django-sql-stacktrace, "github.com/adw0rd/django-sql-stacktrace";. Beside this django.backend.db logger that you can configure in

Django ORM with date_trunk function and timezones

半世苍凉 提交于 2019-12-12 10:44:33
问题 I would like to use date_trunc SQL function but it it doesn't seem to work with timezones. Test 1 with Django : from django.db import connection cursor = connection.cursor() cursor.execute(""" SELECT (date_trunc('day', when_start)) AS "d", COUNT("stats_histo_call"."id") AS "agg" FROM "stats_histo_call" WHERE ("stats_histo_call"."offre_id" = 28 AND "stats_histo_call"."when_start" BETWEEN '2014-08-04 00:00:00+02:00' and '2014-08-08 23:59:59.999999+02:00') GROUP BY (date_trunc('day', when_start)

django & sql - how can I efficiently store and update sort order information associated with records in my database table?

血红的双手。 提交于 2019-12-11 13:56:59
问题 So I have a Django app and one of the tables is basically a list of items. The user may choose to rearrange the order of this list. When they do this, I want to preserve this information so that the next time they access the app the sort order appears as they specified. My first instinct was to just associate an ordering number with each item in the list. Then, if the user moves an item from position 5 to position 2, I would update the order number associated with that record. I immediately

Django postgres Json field with numeric keys

烈酒焚心 提交于 2019-12-11 00:31:26
问题 I have model with postgres json field. class MyModel(models.Model): data = JSONField(null=True) then, I do: m1 = MyModel.objects.create(data={'10':'2017-12-1'}) m2 = MyModel.objects.create(data={'10':'2018-5-1'}) I want query all the MyModel whose key '10' starts with '2017', so I want to write: MyModel.objects.filter(data__10__startswith='2017') The problem is that the 10 is interpreted as integer, and therefore, in the generated query it is considered as list index and not key. Is there