django sitemap.xml,用于数百万个生产

发布于 2025-02-05 08:21:57 字数 988 浏览 2 评论 0原文

我们有大量产品。约5000万件 在Django生成站点地图的最佳方法是什么?

目前,我们以以下方式生成它们:

sitemaps.py

class BookSitemap1(Sitemap):
    protocol = 'https'
    changefreq = 'weekly'
    priority = 0.9

    def items(self):
        return Book.objects.all().order_by('id')[0:25000]

class BookSitemap2(Sitemap):
    protocol = 'https'
    changefreq = 'weekly'
    priority = 0.9

    def items(self):
        return Book.objects.all().order_by('id')[25000:50000]

...

urls.py

sitemap_books_1 = {'books1': BookSitemap1}
sitemap_books_2 = {'books2': BookSitemap2}

...

path('books-1.xml', sitemap, {'sitemaps': sitemap_books_1}, name='django.contrib.sitemaps.views.sitemap'),
path('books-2.xml', sitemap, {'sitemaps': sitemap_books_2}, name='django.contrib.sitemaps.views.sitemap'),

...

等我们所有5000万种产品。但这是2000个地点...我们可以在每个站点地图中放置50000 ULR。但这将再次是1000个站点地图,

还有其他解决方案可以在Django中生成站点地图吗?因为这种解决方案非常不便

We have a big amount of products. About 50 million items
What is the best way to generate sitemaps in Django?

At the moment we generate them the following way:

sitemaps.py

class BookSitemap1(Sitemap):
    protocol = 'https'
    changefreq = 'weekly'
    priority = 0.9

    def items(self):
        return Book.objects.all().order_by('id')[0:25000]

class BookSitemap2(Sitemap):
    protocol = 'https'
    changefreq = 'weekly'
    priority = 0.9

    def items(self):
        return Book.objects.all().order_by('id')[25000:50000]

...

Urls.py

sitemap_books_1 = {'books1': BookSitemap1}
sitemap_books_2 = {'books2': BookSitemap2}

...

path('books-1.xml', sitemap, {'sitemaps': sitemap_books_1}, name='django.contrib.sitemaps.views.sitemap'),
path('books-2.xml', sitemap, {'sitemaps': sitemap_books_2}, name='django.contrib.sitemaps.views.sitemap'),

...

And so on for all our 50 million products. But this is 2000 sitemaps... We can put 50000 ulrs in each sitemap. But this will be 1000 sitemaps again

Is there any other solution to generate sitemaps in Django? Because this solutions is very inconvenient as for me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

但可醉心 2025-02-12 08:21:58

如果您将项目()作为QuerySet返回,Django将为您处理分页。

class BookSitemap(Sitemap):
protocol = 'https'
changefreq = 'weekly'
priority = 0.9

def items(self):
    return Book.objects.all().order_by('id')

然后让Django stitemaps.views.Index处理您的索引:

path('sitemap.xml', index, {'sitemaps': {'books': BookSitemap}}),
path('sitemap-<section>.xml', sitemap, {'sitemaps': {'books': BookSitemap}}, name='django.contrib.sitemaps.views.sitemap')

If you return items() as a QuerySet, Django will handle the pagination for you.

class BookSitemap(Sitemap):
protocol = 'https'
changefreq = 'weekly'
priority = 0.9

def items(self):
    return Book.objects.all().order_by('id')

Then let Django sitemaps.views.index handle your index:

path('sitemap.xml', index, {'sitemaps': {'books': BookSitemap}}),
path('sitemap-<section>.xml', sitemap, {'sitemaps': {'books': BookSitemap}}, name='django.contrib.sitemaps.views.sitemap')

Related: Ping google about paginated sitemap django

影子是时光的心 2025-02-12 08:21:58

@thiago Mattos关于使用SiteMap索引的答案是正确的。

但是,为了使其有效,我宁愿摆脱book.objects.all()并使用.values_list(“ id_or_or_necction_field”)而不是。

因为sitemap正在使用引擎盖下的Paginator,无论如何都会通过整个Objects_list迭代您正在插入的整个objects_list,在您的情况下,它是querySet可以很大。

@Thiago Mattos is right in his answer about using sitemap index.

But to make it a bit memory efficient, I would rather get rid of the Book.objects.all() and use .values_list("id_or_necessary_field") instead.

Because Sitemap is using paginator under the hood and will anyway iterate through the whole objects_list you're inserting there, in your case it's Queryset, which can be quite large.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文