0

Views:

def home(request):
     terminate = Tvserie.objects.filter(status='Terminata' or status='Cancellata').order_by('?')[0:4]

     return render_to_response('Home.html',{'terminate':terminate})

It is an error, why? How do I create a multiple filter of the attribute "status"?

Models:

class Tvserie(models.Model):
      status_choices = (('Non iniziata', 'Non iniziata'),('In corso', 'In corso'),
                        ('In pausa', 'In pausa'),('Rinnovata', 'Rinnovata'),
                        ('Cancellata', 'Cancellata'),('Terminata', 'Terminata'))
      status = models.CharField(max_length = 30, choices=status_choices)
Pang
  • 9,564
  • 146
  • 81
  • 122
JacopoWoty
  • 21
  • 3

2 Answers2

0

You can use in:

 terminate = Tvserie.objects.filter(status__in=['Terminata''Cancellata']).order_by('?')[0:4]
-1

Here is the answer to your question :)

Q objects

Instead of passing explicit parameter you want to be used in filtering, you pass a Q object that encapsulates such information. Moreover, you can use | and & operators to join them by or and and operators.

In your case, it would look like this:

from django.db.models import Q

terminate = Tvserie.objects.filter(Q(status='Terminata') | Q(status='Cancellata')).order_by('?')[0:4]
MateuszPrzybyla
  • 897
  • 8
  • 17