Reputation: 6831
I have this
from django.db import models
class Kid(models.Model):
name = models.CharField(max_length=200)
class Toy(models.Model):
name = models.CharField(max_length=200)
owner = models.ForeignKey(Kid, related_name="kid_toys")
Now each kid has many toys.
I want to find those kids whose last toy bought has name which contains new
I'm trying like this
Kid.kids_toys.order_by('-created_date').first().filter(name__icontains='new')
Upvotes: -1
Views: 224
Reputation: 3215
kids = Kid.objects.filter(kid__toys__in=Toy.objects.filter( name__icontains='new').order_by('-created_date').first() )
Upvotes: 0
Reputation: 10135
Something like this:
kids = Kid.objects.filter(kid_toys=Toy.objects.filter(
name__icontains='new').order_by('-created_date').first()
)
Upvotes: 3