1

I am trying to use Django sitemaps.

class BlogSiteMap(Sitemap):
    """A simple class to get sitemaps for blog"""

    changefreq = 'hourly'
    priority = 0.5

    def items(self):
        return Blog.objects.order_by('-pubDate')

    def lastmod(self, obj):
        return obj.pubDate

My problem is..I wanted to set the priority of first 3 blog object as 1.0 and rest of them as 0.5 priority.

I read the documentation but couldn't any way out of it.

Any help would be appreciable. Thanks in advance.

aatifh
  • 2,317
  • 4
  • 27
  • 30

2 Answers2

1

I think you can alter each object with its priority. Like that for example:

def items(self):
    for i, obj in enumerate(Blog.objects.order_by('-pubDate')):
       obj.priority = i < 3 and 1 or 0.5
       yield obj

def priority(self, obj):
    return obj.priority
Alex Koshelev
  • 16,879
  • 2
  • 34
  • 28
  • I have the same problem but a little bit different: https://stackoverflow.com/questions/64172520/how-to-have-different-change-frequency-and-priority-for-a-list-of-items-in-djang – Amin Ba Oct 02 '20 at 15:31
0

Something like that might work:

def priority(self, obj):
    if obj.id in list(Blog.objects.all()[:3].values_list('id'))
        return 1.0
    else:
        return 0.5
webjunkie
  • 6,891
  • 7
  • 46
  • 43