Use this tag for PostgreSQL issues which are to be solved in accordance with Django coding best practices.
Questions tagged [django-postgresql]
207 questions
2
votes
1 answer
Using Django's Case/When queryset conditions to set more than one value?
I have two values that depend on the same case when condition in my sql call. Currently I duplicate the condition to set each value separately, but can I combine the two identical cases to set both values at once?
edit - added an example:
…

be2213444
- 607
- 1
- 4
- 10
2
votes
1 answer
Django postgress gives psycopg2.errors.NotNullViolation: null value in column "id_id" violates not-null constraint
Below is my model
Windows 10, postgress 12, django 3.0.7, python 3.7.6
class User(AbstractBaseUser, PermissionsMixin):
email = models.CharField(max_length=30)
password = models.CharField(max_length=4000)
first_name =…

AMendis
- 1,346
- 4
- 18
- 34
2
votes
1 answer
SearchRank on multiple SearchVectorFields
I'm trying to integrate full text search in my application. Referencing Django 3.1 documentation
If I want to do weighted search across several fields I should do the following:
from django.contrib.postgres.search import SearchQuery, SearchRank,…

Zarrie
- 325
- 2
- 16
2
votes
2 answers
How to search in a JSONField for a key with spaces in Django with Postgres?
Suppose I've a JSONField namely "data" defined in one of my models.py in Django. The Contents of the field is somewhat similar to the following -
{
"name": "John",
"email": "johndoe@foo.bar",
"last name": "Doe"
}
I need to write a…

deltaforce
- 524
- 1
- 8
- 25
2
votes
0 answers
Django aggregation function in Hstore field
I am using djagno postgres function from
https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/
I need to use aggregation function in hstore field, but getting error...
Here is my models.py
def get_default_dict():
return…

Somil
- 1,921
- 1
- 21
- 35
2
votes
0 answers
Django Json Field - Find objects hat do not have key
I have a Model with a JSON Field.
data = JSONField(default=dict)
Is there an efficient way - or queryset filter - to find all instances of this Model that do not have a speicfic key in the json field.
Django documentation includes has_key function…

Dan Walters
- 1,218
- 1
- 18
- 30
2
votes
2 answers
Migrate command create all tables on second DB - Django
I have an app (ali) on my project (website) and I wanted it to have its own database. The problem is, when I run python manage.py migrate --database=ali, the command recreates all tables within my ali database; whereas the expected result would be…

FTM
- 1,887
- 17
- 34
2
votes
1 answer
Django - Annotate and cast an encrypted TextField to a FloatField to 2 decimal places
I'm using Django pgcrypto fields to encrypt an amount value in a model
Invoice as follows:
from pgcrypto import fields
class Invoice(models.Model):
# Some other fields
amount_usd = fields.TextPGPSymmetricKeyField(default='')
objects =…

Mehak
- 961
- 9
- 20
2
votes
1 answer
Aggregate over foreign key attributes
I want to know the number of questions that has been asked to a specific user.
Here are my models
class TrueOrFalseQuestion(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
…

Florian Thuin
- 920
- 1
- 7
- 19
2
votes
1 answer
Django postgres Json field with numeric keys
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…

eran
- 6,731
- 6
- 35
- 52
2
votes
2 answers
Format Django QuerySet to output values as an array
I have data in a table that look like this:
src_id, dst_id, params
int , int , array
I'm querying the data to extract some values from the array
with the following Django query
dataset = query_set.values_list('src_id', 'dst_id', *[e.field for e…

MrE
- 19,584
- 12
- 87
- 105
2
votes
1 answer
Django DateField to infinity get 'OverflowError: date value out of range'
In my Django model a DateField(representing an expiredate) could by infinite, i use as infinite date the calue of 'datetime.datetime.max', but what i get is
OverflowError: date value out of range
The db i'm using is postgresql.
Why this error?…

Giuseppe
- 363
- 5
- 19
2
votes
2 answers
DjangoORM: Resolve F Expression in Custom DB Function
I am trying to write a custom PostgreSQL function in Django that will coerce datetimes to a specified timezone inside of a queryset. My first pass at the db function looks like this:
from django.db.models.expressions import Func
class…

chukkwagon
- 614
- 6
- 11
2
votes
1 answer
django.contrib.postgres.fields with SQLite
In Django 1.8, what happens if I try to run unit tests against a SQLite DB when the model has fields from django.contrib.postgres.fields?

Alex Rothberg
- 10,243
- 13
- 60
- 120
2
votes
1 answer
django & sql - how can I efficiently store and update sort order information associated with records in my database table?
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…

tadasajon
- 14,276
- 29
- 92
- 144