Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
El nuevo administrador de Django escrito no funciona en la plantilla

Tengo una aplicación de lista de tareas simple donde cada tarea tiene un campo booleano llamado is_deleted. en mi plantilla de lista, solo quiero mostrar las tareas con is_deleted=False .

Sé que puedo usar Task.objects.all().filter(is_deleted=False) en mi opinión; pero quiero hacerlo con gerentes en mi plantilla . aquí está mi gerente:

 class TaskManager(models.Manager): def available(self): return self.filter(is_deleted=False) . . objects = TaskManager()

aquí está mi vista:

 class TaskList(ListView): model = models.Task context_object_name="tasks" template_name = "home/list.html" paginate_by=5

y aquí está la condición en mi plantilla:

 {% if tasks.available%} ...
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Mi respuesta es un complemento/una corrección de su trabajo:

  • tu gerente esta bien

  • Pero debe especificar en el modelo de tareas que anule el administrador predeterminado por su administrador personalizado de esta manera:

     Class Task(models.Model): # others fields here # override the default objects manager objects = TaskManager() # You can also define a new manager and keep the default objects, in the case you will use it later. # objects = models.Manager() -> Task.objects.all() give all objects # is_available = TaskManager() -> Task.is_available.all() give only nt deleted task.
  • en tus vistas.py

     class TaskList(ListView): model = models.Task context_object_name="tasks" template_name = "home/list.html" paginate_by=5 # If you override the default objects manager with TaskManager # Then you are nothing to do, {{ tasks }} is the queryset in the template # If you defined two managers as recommended above, then you need # to override the queryset attribute of the ListView queryset = Task.is_available.all() # Explicit call of custom manager
  • De todos modos, en su plantilla, no necesita la etiqueta {% si %} para filtrar los resultados, todas sus tareas están disponibles en el nombre del valor context_object_name .

     {% for task in tasks %} {{ task.name }} ... {% endfor %}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!