skip to Main Content

Mysql – Slow performance when order by field in foreign key

When a queryset is sorted by a field in a related model performance decreases drastically. I use mysql. For example, I have two models: class Event(models.Model): idEvent = models.BigAutoField(primary_key=True) created_at = models.DateTimeField(db_index=True, verbose_name=_('date')) processed_at = models.DateTimeField(auto_now_add=True, verbose_name=_('processed')) data = models.TextField()…

VIEW QUESTION

Aggregate (nested) JSON key value of JSONField Django PostgreSQL

I have a simple model setup as below, import random import string from django.db import models def random_default(): random_str = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) return {"random": random_str, "total_price": random.randint(1, 100)} class Foo(models.Model): cart = models.JSONField(default=random_default) I want…

VIEW QUESTION

Groupby using Django's ORM to get a dictionary of lists from the queryset of model with foreignkey – Postgresql

I have two models, Business and Employee: from django.db import models class Business(models.Model): name = models.CharField(max_length=150) # ... class Employee(models.Model): business = models.ForeignKey( Business, related_name="employees", on_delete=models.CASCADE, ) name = models.CharField(max_length=150) # ... Here's a sample data: Business.objects.create(name="first company") Business.objects.create(name="second company")…

VIEW QUESTION
Back To Top
Search