在 DRF api 端点中添加其他数据
Adding additional data in the DRF api endpoint
我的 endpoints.py 有:
from wagtail.api.v2.endpoints import BaseAPIEndpoint
from .models import Week, Ingredient, Menu
class WeekAPIEndpoint(BaseAPIEndpoint):
model = Week
def register_endpoints(api_router):
api_router.register_endpoint('week', WeekAPIEndpoint)
如果我按照 link 127.0.0.1:8000/api/v2/week 我得到这个:
是否可以在 DRF 端点中一步提升数据?
从这里 127.0.0.1:8000/api/v2/week/1:
我在 github 中打开了 issue。不想在这里提出太大的问题。
正如@Oleg here 所建议的那样,通过在端点中添加 1 行代码解决了问题:
class WeekAPIEndpoint(BaseAPIEndpoint):
model = Week
listing_default_fields = BaseAPIEndpoint.listing_default_fields + ['year', 'week']
所以它需要我的 "year" 和 "week" 字段并拾取它们,所以我的 api 端点看起来不错谢谢@oleg。
我的 endpoints.py 有:
from wagtail.api.v2.endpoints import BaseAPIEndpoint
from .models import Week, Ingredient, Menu
class WeekAPIEndpoint(BaseAPIEndpoint):
model = Week
def register_endpoints(api_router):
api_router.register_endpoint('week', WeekAPIEndpoint)
如果我按照 link 127.0.0.1:8000/api/v2/week 我得到这个:
是否可以在 DRF 端点中一步提升数据? 从这里 127.0.0.1:8000/api/v2/week/1:
我在 github 中打开了 issue。不想在这里提出太大的问题。
正如@Oleg here 所建议的那样,通过在端点中添加 1 行代码解决了问题:
class WeekAPIEndpoint(BaseAPIEndpoint):
model = Week
listing_default_fields = BaseAPIEndpoint.listing_default_fields + ['year', 'week']
所以它需要我的 "year" 和 "week" 字段并拾取它们,所以我的 api 端点看起来不错谢谢@oleg。