On Mon, 2007-05-07 at 22:32 +0000, roderikk wrote: > Hi all, > > I have recently started with django and have been able to do good work > with the excellent documentation. Thank you. > > However, now I am a little baffled. I have created a gallery app. The > model exists of a Gallery and Photo which has a foreign key to the > Gallery. > > Now if I want to use the function photo.get_next_by_date in a template > and there is a photo in another gallery which has that next date it > will return that photo instead of the next one in the gallery. > > In my view there are a number of things I could do but I would like to > hear from you which is best... > > First of all, if possible can I overwrite the get_next_by_FOO() > function so it checks that the gallery is the same, but how?
I wouldn't override this function. Just write your own function with whatever name you like that does the work you want. The get_next_by_FOO() auto-functions are useful in the case of a particular ordered type of field. If they don't do what you want, just write another method that does the "right thing". The name is not important, but I would avoid overriding and automatically created function so that you don't have to always think about whether the automatically created thing is being called or your version (it's harder to debug). > > Second, I could create a page function in my Photo model as such: > > def page(self): > photo_list = [x for x in self.gallery.photo_set.all()] > ind = photo_list.index(self) > return ind + 1 > > And create a new template tag 'get_next_by_page' which checks for the > page/index. Would also work. > > Finally, in the template, which is fed with and object_list, I could > also try to go back in the for loop (again, don't know how) and just > get the previous/next objects in the list like so: > > {% for photo in object_list %} > {% if has_next %} > {# nextPhoto is object_list[currentCounter + 1 ] ? #} > {% endif %} > {% endfor %} You can't do complex processing like this in templates. It's a sign that you should be making things easier for yourself back in the view or in the model, as in one of your previous suggestions. Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---