django sitemap.xml,用于数百万个生产
我们有大量产品。约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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将项目()作为QuerySet返回,Django将为您处理分页。
然后让Django stitemaps.views.Index处理您的索引:
If you return items() as a QuerySet, Django will handle the pagination for you.
Then let Django sitemaps.views.index handle your index:
Related: Ping google about paginated sitemap django
@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 wholeobjects_list
you're inserting there, in your case it'sQueryset
, which can be quite large.