Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

317
Vistas
Django - working with queryset

My models currently look like this:

class Dealer(models.Model):
    name = models.CharField(unique=True, max_length=255, default='')
    url = models.URLField()

    def __str__(self):
        return self.name


class Category(models.Model):
    name = models.CharField(unique=True, max_length=255, default='')

    def __str__(self):
        return self.name


class Car(models.Model):
    name = models.CharField(unique=False, max_length=255,default='')
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Price(models.Model):
    car = models.ForeignKey(Car, default='')
    dealer = models.ForeignKey(Dealer, default='')
    price = models.DecimalField(max_digits=9, decimal_places=2)
    url = models.URLField(max_length=255)

    def __str__(self):
        return str(self.price)

What I want to be able to do is answer the following question (something that will ultimately become the context of my view): Show me a list of cars in a particular category that are available at different dealerships, with their prices. The query that I'm using for testing is this:

carprice = Price.objects.filter(car__category='1').values('car__name','price','dealer__name').order_by('car__name')

The resulting QuerySet looks something like this:

<QuerySet [{'car__name': 'Audi Model A', 'price': Decimal('32000.00'), 'dealer__name': 'Dealer A'}, {'car__name': 'Audi Model A', 'price': Decimal('35000.00'), 'dealer__name': 'Dealer B'}, {'car__name': 'Audi Model A', 'price': Decimal('35000.00'), 'dealer__name': 'Dealer C'}, '...(remaining elements truncated)...']>

Question 1: Can the query be modified so that the QuerySet does not repeat the car__name every time but instead groups prices and dealers by car__name? If someone could point me to a good tutorial/documentation on QuerySets, I would be really grateful. I've tried looking at Django's documentation and couldn't really find an answer to this question.

- Audi Model A
    - Dealer A 32000.00
    - Dealer B 35000.00
    - Dealer C 35000.00

Question 2: With the current query, how can the grouping be done in the view function to achieve the look above?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

For anyone visiting this page looking for an answer, here's the best I could come up with:

Question 1: Based on my review of Django documentation and the interaction I had with another Django developer, the answer is (probably) no.

Question 2: The answer is: regroup template tag. Reference

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda