I'd like to reduce actual database hits when working in django so I've created this artificial example to outline the problem. The model looks like this:
class Car(models.Model):
name = models.CharField(max_length=10)
user = models.ForeignKey(User)
In my view I want to do something like this:
def cartest(request):
cars = Car._default_manager.filter(user__username__exact='michael')[:5]
first_car_name = cars[0].name
another_car = cars[1]
return HttpResponse(len(connection.queries))
So I want to select 5 entries from the database, do something with the first one, then do something with the second one (remember this is an artificial example). There must be some way to do this without hitting the database twice, right?
Thanks, Mike