Let's say I have a ListView
that looks like this:
class CarsListView(ListView):
model = Car
paginate_by = 5
and a model Car
:
class Car(models.Model):
created = models.DateTimeField()
brand = models.CharField(max_length=30)
class Meta:
ordering = ("created",)
The CarsListView
is working correctly showing me all cars ordered by created
. So far, so good.
Instead of creating a DetailView
for each car, I want to add a view that redirects incoming requests for a specific car to the CarsListView
on the correct page.
class CarsRedirectView(RedirectView):
def get_redirect_url(self, *args, **kwargs):
car = Car.objects.get(pk=kwargs["pk"])
# stuck here
return "hmm?"
All I really need to know here is on which page the car is. One way to do this is to fetch all Car objects and count the pages until I hit the car.
Is there a better way?