json-ld:为自定义值建模的好方法
json-ld: Good way to model custom values
我正在尝试获得一个很好的 json-ld,它结合了架构。org/Product 定义和一些自定义元素。
我来自 xsd 背景,json-ld 的可扩展性似乎很难实现。
我从在 Google (https://developers.google.com/search/docs/guides/search-gallery) 找到的产品模板标记开始并尝试扩展它(我想向它添加类似 mydomain:tags 的东西)但是我我不知道该怎么做。
<script type="application/ld+json">
{
"@context": ["http://schema.org/",
{"mydomain": "http://mystuff.com/"}],
"@type": "Product",
"name": "Executive Anvil",
"image": "http://www.example.com/anvil_executive.jpg",
"description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
"mpn": "925872",
"brand": {
"@type": "Thing",
"name": "ACME"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.4",
"reviewCount": "89"
},
"offers": {
"@type": "Offer",
"priceCurrency": "USD",
"price": "119.99",
"priceValidUntil": "2020-11-05",
"itemCondition": "http://schema.org/UsedCondition",
"availability": "http://schema.org/InStock",
"seller": {
"@type": "Organization",
"name": "Executive Objects"
}
},
"mydomain:tags" : {}
}
</script>
任何关于我在这里做错了什么的线索都将不胜感激。
这可能是愚蠢的...
您的 JSON-LD 似乎是正确的。您正在使用 example 19 (Compact IRIs) and example 29 (Advanced Context Usage).
的组合
Google 的结构化数据测试工具不是通用的 JSON-LD 验证器。它报告的错误主要是针对其搜索结果功能。他们的错误 ("The property http://mystuff.com/tags
is not recognized by Google for an object of type Product
.") 只是说它不是 Google 知道的属性之一,这当然是正确的。
如果您想验证您的 JSON-LD,而不会收到 Google 特定功能的错误,您可以使用 http://json-ld.org/playground/,例如。
如果你想在 Django 中为你的 ListView 和 DetailView 使用 JsonLd,那么你不需要为从管理端添加的所有列表项编写它,你只需要在列表视图中传递 JsonLdListView class 和 DetailView 中的 JsonLdDetailView class 以及模型中的一个函数
第一步
在 models.py 中,在您为其创建 ListView 和 DetailView 的模型中编写此函数
@property
def sd(self):
return {
"@type": 'Organization',
"description": self.description,
"name": self.name,
}
*name 和 description 是同一个模型的字段名
from django_json_ld.views import JsonLdDetailView, JsonLdListView
第 2 步
class PortfolioListView(JsonLdListView, ListView):
pass
第 3 步
class PortfolioDetailView(JsonLdDetailView, DetailView):
def get_structured_data(self):
sd = super(DesignzPortfolioDetailView,
self).get_structured_data()
return sd
我正在尝试获得一个很好的 json-ld,它结合了架构。org/Product 定义和一些自定义元素。
我来自 xsd 背景,json-ld 的可扩展性似乎很难实现。
我从在 Google (https://developers.google.com/search/docs/guides/search-gallery) 找到的产品模板标记开始并尝试扩展它(我想向它添加类似 mydomain:tags 的东西)但是我我不知道该怎么做。
<script type="application/ld+json">
{
"@context": ["http://schema.org/",
{"mydomain": "http://mystuff.com/"}],
"@type": "Product",
"name": "Executive Anvil",
"image": "http://www.example.com/anvil_executive.jpg",
"description": "Sleeker than ACME's Classic Anvil, the Executive Anvil is perfect for the business traveler looking for something to drop from a height.",
"mpn": "925872",
"brand": {
"@type": "Thing",
"name": "ACME"
},
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "4.4",
"reviewCount": "89"
},
"offers": {
"@type": "Offer",
"priceCurrency": "USD",
"price": "119.99",
"priceValidUntil": "2020-11-05",
"itemCondition": "http://schema.org/UsedCondition",
"availability": "http://schema.org/InStock",
"seller": {
"@type": "Organization",
"name": "Executive Objects"
}
},
"mydomain:tags" : {}
}
</script>
任何关于我在这里做错了什么的线索都将不胜感激。 这可能是愚蠢的...
您的 JSON-LD 似乎是正确的。您正在使用 example 19 (Compact IRIs) and example 29 (Advanced Context Usage).
的组合Google 的结构化数据测试工具不是通用的 JSON-LD 验证器。它报告的错误主要是针对其搜索结果功能。他们的错误 ("The property http://mystuff.com/tags
is not recognized by Google for an object of type Product
.") 只是说它不是 Google 知道的属性之一,这当然是正确的。
如果您想验证您的 JSON-LD,而不会收到 Google 特定功能的错误,您可以使用 http://json-ld.org/playground/,例如。
如果你想在 Django 中为你的 ListView 和 DetailView 使用 JsonLd,那么你不需要为从管理端添加的所有列表项编写它,你只需要在列表视图中传递 JsonLdListView class 和 DetailView 中的 JsonLdDetailView class 以及模型中的一个函数
第一步 在 models.py 中,在您为其创建 ListView 和 DetailView 的模型中编写此函数
@property
def sd(self):
return {
"@type": 'Organization',
"description": self.description,
"name": self.name,
}
*name 和 description 是同一个模型的字段名
from django_json_ld.views import JsonLdDetailView, JsonLdListView
第 2 步
class PortfolioListView(JsonLdListView, ListView):
pass
第 3 步
class PortfolioDetailView(JsonLdDetailView, DetailView):
def get_structured_data(self):
sd = super(DesignzPortfolioDetailView,
self).get_structured_data()
return sd