你好。
需要在不同的 URL 上提供模型的实例。例如,让一组商品在所有城市都有售。
为清楚起见:
class Product(models.Model):
cities = models.ManyToManyField(City)
假设有30个城市,150种商品,如果我选择所有商品,我得到150个对象,如果全部30个城市,那么30个对象。
综合而言,我制作的 url 可以接受城市和产品:
urlpatterns = [
url(
regex=r'^(?P<city>[\w.@+-]+)/(?P<product>[\w.@+-]+)/$',
view=views.CityProductView.as_view(),
name='city_product'
)
]
在视图中,是这样的:
class ProductCity(DetailView):
model = Product
def get_object(self):
product = get_object_or_404(Product city__name__iexact=self.kwargs['city'])
return service
事实上,网站本身提供了 30 * 150 个页面,这正是所需要的。
问题:
如何将所有这些网址提供给sitemap?
如有必要,在页面参数(过滤器)sitemap下生成完全相同的问题。GET
如果该方法items可以执行以下操作:
def items(self):
items = []
cities = City.objects.all()
for city in cities:
products = Product.objects.filter(cities__in=[city])
items.extend(products)
return items
怎么办location,应该返回get_absolute_url()?
他们如何在公告牌上做到所有类别都在每个城市?我不太明白其中的逻辑。
我习惯于编写 get_absolute_url() 模型并为此方法编写测试,但在这种情况下呢?
提前致谢!
有一个可行的解决方案:
在模型中创建了一个方法,例如
get_sitemap_urls:那些。事实上,我们生成了一个基于所有城市的列表,在本例中,是与模型相关联的城市。
然后
sitemap.py改变方法items和location不幸的是,我还没有想出如何让这个方法保持活力
lastmod