Questions tagged [django-postgresql]

Use this tag for PostgreSQL issues which are to be solved in accordance with Django coding best practices.

207 questions
3
votes
2 answers

How to filter by the last element of a json array field in Django

I have a model MyModel with a JSONField called data. This json field contains an array of objects like: [ { "date": "2021-01-01", "amount": 120 }, { "date": "2021-01-02", "amount": 150 } ] I would…
nbeuchat
  • 6,575
  • 5
  • 36
  • 50
3
votes
2 answers

pandas to_sql in django: insert foreign key into DB

Is there a way to insert foreign keys when using pandas to_sql function? I am processing uploaded Consultations (n=40k) with pandas in django, before adding them to the database (postgres). I got this working row by row, but that takes 15 to 20…
3
votes
1 answer

Annotation with a subquery with multiple result in Django

I use postgresql database in my project and I use below example from django documentation. from django.db.models import OuterRef, Subquery newest =…
3
votes
3 answers

Fast moving average computation with Django ORM

We run Postgres 9.6.5 and Django 2.0. We have a Model with fields created_at and value. We need to calculate a 90-day moving average for a certain date_range. This is how we do this: output = [] for i in range(len(date_range)): output.append( …
petr
  • 1,099
  • 1
  • 10
  • 23
3
votes
1 answer

SELECT typarray FROM pg_type WHERE typname = 'citext'

SELECT typarray FROM pg_type WHERE typname = 'citext' Why I am getting this query in django debug panel and what does it mean? Whenever I navigate to the new page, this query gets run as 1st then all others, same thing in the python shell using…
Horion
  • 142
  • 8
3
votes
1 answer

ArrayField to store custom Field value

I want to use an arrayfield to store a list of json strings which represent a specific custom class. What I have done is: from django.contrib.postgres.fields import JSONField, ArrayField from django.db import models # other imports class…
stelios
  • 2,679
  • 5
  • 31
  • 41
3
votes
2 answers

Django Queryset: column reference is ambiguous with extra queryset

I have the next queryset: Article.objects.filter(finished=True, updated_at__range=[start, end]).extra( select={'hour': 'extract(hour from updated_at)'}).values('hour').annotate(categorized=Count('id')) And I get the error : "ProgrammingError:…
Andrés Root
  • 642
  • 1
  • 7
  • 11
3
votes
2 answers

How to use ArrayField in Django using PostgreSQL DB?

I want to do this import in my django models.py: from django.contrib.postgres.fields import ArrayField I read this documentation https://docs.djangoproject.com/en/dev/ref/contrib/ and I added 'django.contrib.postgres' into my INSTALLED_APPS in…
Matúš Bartko
  • 2,425
  • 2
  • 30
  • 42
3
votes
1 answer

Connection to Django default failed

So i'm using Pycharm and have installed the Django framework and Postgres. When I initially setup the default Django DB to point to the Postgres database everything works fine. The problem occurs if I make any changes to a table, meaning the…
Jeff E
  • 105
  • 1
  • 5
3
votes
1 answer

How to efficiently write DISTINCT query in Django with table having foreign keys

I want to show distinct cities of Users in the front end dropdown. For that, i make a db query which fetches distinct city_name from table City but only those cities where users are present. Something like below works for a small size of User…
prat0318
  • 571
  • 1
  • 8
  • 19
3
votes
1 answer

Django filter - iregex to match complete word only

So I've been trying to implement a search feature where by a user can enter a keyword and look up users. The search method is supposed to look up exact word matches within the fields of the users e.g first name, last name, job etc I have tried…
Ahsan Saleem
  • 43
  • 1
  • 7
3
votes
1 answer

Django (postgresql-psycopg2) connection pool: Simple vs Threaded vs Persistent ConnectionPool

I was looking at db connection pool implementations to use in my Django app using postgresql db backend, and came across this very recent wrapper package implemented for psycopg2 - https://pypi.python.org/pypi/django-db-pool psycopg2 in itself…
3
votes
0 answers

South failing to perform migration for tastypie api key tables

I'm trying to perform a database migration to create the necessary tables for API Key authentication via tastypie and south. This requires that I run python manage.py migrate tastypie. The output is: Running migrations for tastypie: Migrating…
3
votes
2 answers

Failure with syncdb --migrate on PostgreSQL database. Circular Dependency

I'm trying to create an empty copy of my Django database using the command manage.py syncdb --migrate --database= It works fine if the database is a sqlite3 database, but fails for a postgres database. With postgres the normal…
JivanAmara
  • 1,065
  • 2
  • 10
  • 20
3
votes
2 answers

Point manage.py to a specific PostegreSQL schema

How can I tell manage.py to operate in a specific PostgreSQL schema? My database is separated into multiple schema, one per client/deployment, to keep data separate. However, it seems that Django does not really support schemas very well. How can I…
Goro
  • 9,919
  • 22
  • 74
  • 108