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

243
Vistas
Django overriding filter() without change existing code logic

I have a table in production that is integrated everywhere in the system, now I need to add a new column in the table with a default value, but don't want to change all the existing logic, what is the best way to do that?

class People(models.Model):
      name = models.CharField(max_length=20)
      gender = models.CharField(max_length=20)
      class = models.CharField(max_length=20)

in the system, we have this kind of query everywhere

People.objects.filter(gender='male')

People.objects.filter(gender='female', class="3rd")
...

Now we need to add a new field:

class People(models.Model):
      name = models.CharField(max_length=20)
      gender = models.CharField(max_length=20)
      class = models.CharField(max_length=20)
      graduated = models.BooleanField(default=False)

Assume that all the existing data should have graduated is False, so all the existing logic should work if we can add graduated=False on every query, but is there any way we can do so we don't need to change any of the existing code but they will assume graduated=False?

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

0

Yes, you can make a manager such that .objects will only retain People with graduated=False:

class PeopleManager(models.Manager):
    
    def get_queryset(self):
        return super().get_queryset().filter(graduated=False)

class People(models.Model):
    name = models.CharField(max_length=20)
    gender = models.CharField(max_length=20)
    class = models.CharField(max_length=20)
    graduated = models.BooleanField(default=False)
    
    objects = PeopleManager()
    all = models.Manager()

You can use People.all.all() to retrieve all People, and People.objects.all() to retrieve all People that did not graduate.

That being said, I do not recommend this: often People.objects.all() gives the impression that one will retrieve all People. As the Zen of Python says: "explicit over implicit": it is better that the code explains and hints what it is doing, not move filtering somewhere hidden in a manager.

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