在所有可以查到的例子里面,都是使用lookup_field的默认的”pk”来查询模型类对象,但是没有看到自定义lookup_field的文章.
官方对lookup_field和lookup_url_kwarg 的解释:
lookup_field - The model field that should be used to for performing object lookup of individual model instances. Defaults to ‘pk’. Note that when using hyperlinked APIs you’ll need to ensure that both the API views and the serializer classes set the lookup fields if you need to use a custom value.
lookup_url_kwarg - The URL keyword argument that should be used for object lookup. The URL conf should include a keyword argument corresponding to this value. If unset this defaults to using the same value as lookup_field.
通常使用的一个方法:
# 定义model
class BookInfo(models.Model)</