Add only URLs having records to sitemap

I'm trying to add a few custom URLs, all linked to one view, with the parameters passed being different. As an example, I'll be having several URLs to pull up all records having a certain make, so it'll be <root_url>/vehicles/inventory/<make>. What I want to do if to ensure that the URLs actually return some records...only those that do will need to go to the sitemap.

Here's how I've done the class for all makes in a certain year (but some of the URLs don't have any vehicles being returned and I want to eliminate that).

class YearMakeSitemap(Sitemap):
  priority = 1.0
  changefreq = 'weekly'

  def items(self):
    makes = query.order_by('manufacturer_popularity', 'manufacturer').values_list
            ('manufacturer', flat = True)
    years = query.order_by('year').values_list('year', flat = True)
    url = []

    for make in makes:
        for year in years:
            url.append('%s/%s' % (make, year))

    return url

  def lastmod(self, obj):
    return datetime.now()

  def location(self, obj):
    return '/vehicles/inventory/%s/' % obj

How can have the sitemap file having only the URLs that return at least one record as opposed to my current situation?

Answers


How about making this change?

for make in makes:
    for year in years:
        if query.filter(year=year,manufacturer=make).count() > 0:
            url.append('%s/%s' % (make, year))

But I think a more efficient answer would be to do this:

query.order_by("manufacturer_popularity","manufacturer","year").values("manufacturer","year").distinct()

and iterate over that list.


Need Your Help

files bypassing filetype selection in php

php forms file-type

i've got if/else in place to make sure only certain types of audio can be uploaded, but it doesn't seem to be working as other file types(images) are making it past my upload form. What's wrong wit...

Android - zoom in/out RelativeLayout with spread/pinch

android android-layout android-animation

I have an activity with a RelativeLayout and a private class in it, which extends the SimpleOnScaleGestureListener. In the onScale method of the listener I'd like to zoom in/out of the whole layout