Next previous links from a query set / generi views
Hello, I have a quite simple query set and a related generic views: http://dpaste.com/155494/ And template for generating a detail page of a photo. Is there an easy way to have a link to previous | next element in the template without manualy coding a view ? Somthing like a: {% if foto.next_item %} Next {% endif} -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
Yup, if you have non-null date/datetime fields on your model, each model will automatically get "get_next_by_FOO" and "get_previous_by_FOO" methods, where FOO is the name of the datetime field: http://docs.djangoproject.com/en/1.1/ref/models/instances/#django.db.models.Model.get_next_by_FOO Eric On Feb 7, 2010, at 2:34 AM, eaman wrote: Hello, I have a quite simple query set and a related generic views: http://dpaste.com/155494/ And template for generating a detail page of a photo. Is there an easy way to have a link to previous | next element in the template without manualy coding a view ? Somthing like a: {% if foto.next_item %} Next {% endif} -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/django-users?hl=en . -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
On Feb 7, 3:24 am, Eric Abrahamsen wrote: > Yup, if you have non-null date/datetime fields on your model, each > model will automatically get "get_next_by_FOO" and > "get_previous_by_FOO" methods, where FOO is the name of the datetime Well I guess the 'Lazy' optiond is to add a date field to my model and get the free pagination. Or code my own view, of course. Thanks. /eaman [CUT] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
On Feb 7, 2010, at 8:54 PM, eaman wrote: On Feb 7, 3:24 am, Eric Abrahamsen wrote: Yup, if you have non-null date/datetime fields on your model, each model will automatically get "get_next_by_FOO" and "get_previous_by_FOO" methods, where FOO is the name of the datetime Well I guess the 'Lazy' optiond is to add a date field to my model and get the free pagination. Or code my own view, of course. The lazy option would probably be to add get_next() and get_previous() methods to your model, that return an instance based on whatever definition of "next" and "previous" works for you. You might consider some kind of timestamp field for your model, though – you'd be surprised how often that comes in handy… Thanks. /eaman [CUT] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/django-users?hl=en . -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
On Feb 7, 2:56 pm, Eric Abrahamsen wrote: > On Feb 7, 2010, at 8:54 PM, eaman wrote: [CUT] > The lazy option would probably be to add get_next() and get_previous() > methods to your model, that return an instance based on whatever > definition of "next" and "previous" works for you. You might consider > some kind of timestamp field for your model, though – you'd be > surprised how often that comes in handy… Thanks, I've made a try with the free pagination, which is nice and easy but doesn't work out of the box as I would like (but that's probbly me unable to pass an extra parameter to limit the query set...). I'm up to code those two methods, I guess I'll try first to embed them in the model and then to think about of some form of abstraction to use them with others similar models. Generic views are really nice, but I guess it's time for me to dive deeper in models and views. Thanks for the clue. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
> I'm up to code those two methods... If some one is interested in this thread I managed to code these two methods: get_next | get_prev in order to get a previous or next item in a set right from my model: - http://dpaste.com/155961/ - Is there a better way to get the highest 'previous' item then using aggregate(Max('id'))? - I guess the if /else conditional loop that should check the existence of the prev | next item is suboptimal... /eaman -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
On Feb 8, 2010, at 7:56 AM, eaman wrote: I'm up to code those two methods... If some one is interested in this thread I managed to code these two methods: get_next | get_prev in order to get a previous or next item in a set right from my model: - http://dpaste.com/155961/ Now that you've got a date attribute, why not use that for next and previous? If you don't want to do that, you still might consider returning a real object instance, and then giving the model a get_absolute_url() method and calling that in the template. That will save you hardcoding the links in the template. But if it's just an id you want, the following might be more efficient: def get_next(self): all_ids = Foto.objects.filter(galleria = self.galleria, id__gt=self.id).values_list("id",flat=True).order_by("id") try: return min(all_ids) except ValueError: return None Then reverse that (id__lt=self.id and use the max python function) for get_prev() Hope that's helpful, Eric - Is there a better way to get the highest 'previous' item then using aggregate(Max('id'))? - I guess the if /else conditional loop that should check the existence of the prev | next item is suboptimal... /eaman -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/django-users?hl=en . -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Next previous links from a query set / generi views
On Feb 8, 3:38 am, Eric Abrahamsen wrote: [CUT] > Now that you've got a date attribute, why not use that for next and > previous? 1. Date based next and prev go throught the whole photo set, but I prefer next and prev to provide only items inside a gallery. But I guess that's just me unable to pass an extra parameter to the generic view. 2. I'm learning: I wanted to test an other way to get this navigation links, coding some custom methods. > If you don't want to do that, you still might consider returning a > real object instance, Yes this should be better, it could be more userfull if I had to generate a PDF or some other kind of output. > and then giving the model a get_absolute_url() > method and calling that in the template. That will save you hardcoding > the links in the template. - http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolute-url Thanks for the advice: this will make deployment and refactoring easier. > > But if it's just an id you want, the following might be more efficient: > > def get_next(self): > all_ids = Foto.objects.filter(galleria = self.galleria, > id__gt=self.id).values_list("id",flat=True).order_by("id") > try: > return min(all_ids) > except ValueError: > return None > > Then reverse that (id__lt=self.id and use the max python function) for > get_prev() > > Hope that's helpful, > Eric Oh yes thanks a lot, code examples are really useful to me as I'm not yet very familiar with of all this 'snaky' dotted object syntax / traversing; I'll get into the django shell and play a bit with this. Thanks for all this suggestions. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.