当包含资源 DRF returns 我的软删除记录
When including resources DRF returns my soft deleted records
我的 DRF API 支持在 authors
get 请求中包含作者的书籍。
我们的 API 有一个软删除系统,其中 Book(2) 被标记为已删除。
但是当我执行 Book(2) 下面的请求时,仍然包含在响应中。
我希望在此请求的响应中只有 Book(1)。
GET
http:///localhost/authors/2?include=books
API returns 对我来说:
{
"data": {
"type": "Author",
"id": "2",
"attributes": {...},
"relationships": {
"books": {
"meta": {
"count": 2
},
"data": [
{
"type": "Book",
"id": "1"
},
{
"type": "Book",
"id": "2"
}
]
}
}
},
"included": [
{
"type": "Book",
"id": "1",
"attributes": {...}
},
{
"type": "Book",
"id": "2",
"attributes": {...}
}
]
}
我有一个 BaseModel
来处理覆盖 delete
方法的软删除:
class BaseModel(Model):
archive = BoolYesNoField(db_column="archive", default=False, null=False)
created = DateTimeField(db_column="created", auto_now_add=True, null=False)
updated = DateTimeField(db_column="updated", auto_now=True, null=False)
objects = BaseManager()
all_objects = BaseManager(alive_only=False)
def delete(self):
self.archive = True
self.save()
relate_obj_delete(self)
def hard_delete(self):
super(Model, self).delete()
def undelete(self):
self.archive = False
self.save()
经理
class BaseManager(Manager):
def __init__(self, *args, **kwargs):
self.alive_only = kwargs.pop("alive_only", True)
super(BaseManager, self).__init__(*args, **kwargs)
def get_queryset(self):
if self.alive_only:
return BaseQuerySet(self.model).filter(archive=False)
return BaseQuerySet(self.model)
def hard_delete(self):
return self.get_queryset().hard_delete()
AuthorViewSet
class AuthorViewSet(BaseViewSet):
queryset = Author.objects.all()
serializer_class = AuthorSerializer
filterset_class = AuthorFilterSet
序列化器
class AuthorSerializer(BaseSerializer):
books = ResourceRelatedField(many=True, read_only=True)
included_serializers = {
"books": BookSerializer,
}
class Meta(BaseSerializer.Meta):
model = Author
如果有人能指出我正确的方向,我会很有帮助。
可能有一个函数我需要在某处重写,但我似乎找不到它。
您的问题出在您的序列化程序中。默认情况下,Django 将使用 Model._base_manager
to fetch related objects and not your custom manager. You need to specify in your nested ResourceRelatedField 字段你想使用哪个管理器
class AuthorSerializer(BaseSerializer):
books = ResourceRelatedField(
queryset=Book.objects,
many=True,
read_only=True
)
...
我的 DRF API 支持在 authors
get 请求中包含作者的书籍。
我们的 API 有一个软删除系统,其中 Book(2) 被标记为已删除。
但是当我执行 Book(2) 下面的请求时,仍然包含在响应中。
我希望在此请求的响应中只有 Book(1)。
GET
http:///localhost/authors/2?include=books
API returns 对我来说:
{
"data": {
"type": "Author",
"id": "2",
"attributes": {...},
"relationships": {
"books": {
"meta": {
"count": 2
},
"data": [
{
"type": "Book",
"id": "1"
},
{
"type": "Book",
"id": "2"
}
]
}
}
},
"included": [
{
"type": "Book",
"id": "1",
"attributes": {...}
},
{
"type": "Book",
"id": "2",
"attributes": {...}
}
]
}
我有一个 BaseModel
来处理覆盖 delete
方法的软删除:
class BaseModel(Model):
archive = BoolYesNoField(db_column="archive", default=False, null=False)
created = DateTimeField(db_column="created", auto_now_add=True, null=False)
updated = DateTimeField(db_column="updated", auto_now=True, null=False)
objects = BaseManager()
all_objects = BaseManager(alive_only=False)
def delete(self):
self.archive = True
self.save()
relate_obj_delete(self)
def hard_delete(self):
super(Model, self).delete()
def undelete(self):
self.archive = False
self.save()
经理
class BaseManager(Manager):
def __init__(self, *args, **kwargs):
self.alive_only = kwargs.pop("alive_only", True)
super(BaseManager, self).__init__(*args, **kwargs)
def get_queryset(self):
if self.alive_only:
return BaseQuerySet(self.model).filter(archive=False)
return BaseQuerySet(self.model)
def hard_delete(self):
return self.get_queryset().hard_delete()
AuthorViewSet
class AuthorViewSet(BaseViewSet):
queryset = Author.objects.all()
serializer_class = AuthorSerializer
filterset_class = AuthorFilterSet
序列化器
class AuthorSerializer(BaseSerializer):
books = ResourceRelatedField(many=True, read_only=True)
included_serializers = {
"books": BookSerializer,
}
class Meta(BaseSerializer.Meta):
model = Author
如果有人能指出我正确的方向,我会很有帮助。 可能有一个函数我需要在某处重写,但我似乎找不到它。
您的问题出在您的序列化程序中。默认情况下,Django 将使用 Model._base_manager
to fetch related objects and not your custom manager. You need to specify in your nested ResourceRelatedField 字段你想使用哪个管理器
class AuthorSerializer(BaseSerializer):
books = ResourceRelatedField(
queryset=Book.objects,
many=True,
read_only=True
)
...