user16773013
user16773013

Reputation:

How to filter models in django database

Hello i created movies website , this is my models.py

CATEGORY_CHOICES = (
    ("A","ACTION"),
    ("D","DRAMA"),
    ("C","COMEDY"),
    ("R",'ROMANCE'),

class Movie(models.Model):
      title = models.CharField(max_length=100)
      slug = models.SlugField(max_length=255, 
      title_english = models.CharField(max_length=100)
      descritpion = models.TextField(max_length=1000)
      image = models.ImageField(upload_to="movies")
      category = models.CharField(choices=CATEGORY_CHOICES,max_length=1)
      language = models.CharField(choices=LANGUAGE_CHOICES,max_length=30)
      status = models.CharField(choices=STATUS_CHOICES,max_length=2)
      year_of_production = models.TextField(max_length=1000)
      view_count = models.IntegerField(default=0)

and this is my views.py

def home(request):
    user = request.user
    Movies_obj = models.Movie.objects.all().order_by('-id')[:10]
    return render(request,'index.html',{'user':user,'movies':Movies_obj})

in views.py i order last 10 movies , now i want to show only this movies which is DRAMA or Comedy how to filter it ?

Upvotes: 0

Views: 38

Answers (1)

Jarno Lahtinen
Jarno Lahtinen

Reputation: 1703

Category field has values A or D so you need to filter category field with those values.

models.Movie.objects.filter(category__in=['A', 'D']).order_by('-id')[:10]

Upvotes: 1

Related Questions