Say we’re building a Django-based site that clones Medium.com’s URL structure, where you have users and articles. We’d probably have this model: We want to be able to build URLs that look like /<username>/<slug>/. Since we’re going to have billions of articles and zillions …
Tag: postgresql
Prevent DateRangeField overlap in Django model?
Now that Django supports the DateRangeField, is there a ‘Pythonic’ way to prevent records from having overlapping date ranges? Hypothetical use case One hypothetical use case would be a booking system, where you don’t want people to book the same resource at the same time. Hypothetical examp…
Executing multiple statements with Postgresql via SQLAlchemy does not persist changes
This does not work – the update has no effect: Switching the statements performs the update and select successfully: Why does the first not work? It works in Pgadmin. I enabled autocommit with Flask-Sqlalchemy. I am doing a workshop on SQL injection, so please dont rewrite the solution! Answer The way SQLAlch…
sqlalchemy.exc.DataError: (psycopg2.DataError) value too long for type character varying
I Try to save a hashed Password in postgresql database using SQL Alchemy. table script is: and this is the mapping: when i try to insert data, this exception raised : but as you see data is exactly fit too field and there is no error when i execute this query inside pgadmin! I think problem is in my mapping.
How can I serialize a queryset from an unrelated model as a nested serializer?
I’m trying to add a nested serializer to an existing serializer based on some criteria of the parent model, not a foreign key. The use case is to return a ‘Research’ object with an array of ‘ResearchTemplate’ objects that are identified by filtering on a Postgres ArrayField. Mode…
Create a Full Text Search index with SQLAlchemy on PostgreSQL
I need to create a PostgreSQL Full Text Search index in Python with SQLAlchemy. Here’s what I want in SQL: Now how do I do the second part with SQLAlchemy when using the ORM: Answer You could create index using Index in __table_args__. Also I use a function to create ts_vector to make it more tidy and r…
Django: Related model ‘users.UserProfile’ cannot be resolved
I tried running makemigrations and after migrate and I am constantly getting this error: What I was trying to do is Link a UserProfile model to Django’s own User Model: The “Contests” model (as you can see in my installed apps below) uses the User Model as well without any errors. My Install…
Connection refused with postgresql using psycopg2
psycopg2.OperationalError: could not connect to server: Connection refused Is the server running on host “45.32.1XX.2XX” and accepting TCP/IP connections on port 5432? Here,I’ve open my sockets. I googled that I should modify this pg_hba.conf,but in my postgresqlroot files, I didn’t fi…
Django: Query Group By Month
How to calculate total by month without using extra? I’m currently using: django 1.8 postgre 9.3.13 Python 2.7 Example. What I have tried so far. and also this one, the answer seems great but I can’t import the TruncMonth module. Django: Group by date (day, month, year) P.S. I know that this quest…
Django migrate ProgrammingError: syntax error at or near “”
I’m learning django (1.9.2) on a practice web site and a postgresql database with psycopg2. I defined a model with a certain attribute “preview” and later deleted the attribute entirely. Despite having removed it, django seems to be referencing that old definition perhaps from a cache or som…