我有曲目和专辑。我希望能够将曲目标记为已删除,但不将其从数据库中删除。我希望标记为已删除 (is_deleted=True( 的曲目不会显示在嵌套序列化程序的 API 中。
我已经在 TrackSerializer.py 上尝试了自定义的"get_queryset"方法,但没有调用它。
serializers.py
class TrackSerializer(serializers.ModelSerializer):
class Meta:
model = Track
class AlbumSerializer(serializers.ModelSerializer):
tracks = TrackSerializer(many=True)
### How do I limit tracks in the related/nested serializer to tracks on this album that have not been deleted?
models.py
class Track(models.Model):
name = models.CharField()
is_deleted = models.BooleanField(default=False)
album = models.ForeignKey(Album, related_name="tracks")
class Album(models.Model):
name = models.CharField()
我确定需要创建自定义ListSerializer
因为我使用了many=True
这就是我最终得到的serializers.py
class DeletedListSerializer(serializers.ListSerializer):
def to_representation(self, data):
iterable = data.exclude(is_deleted=True) if isinstance(data, models.Manager) else data
return [
self.child.to_representation(item) for item in iterable
]
class TrackSerializer(serializers.ModelSerializer):
class Meta:
model = Track
class AlbumSerializer(serializers.ModelSerializer):
tracks = TrackSerializer(many=True)
### How do I limit tracks in the related/nested serializer to tracks on this album that have not been deleted?
@classmethod
def many_init(cls, *args, **kwargs):
kwargs['child'] = cls()
return DeletedListSerializer(*args, **kwargs)
我不知道你的观点是什么样的。假设您正在使用视图集。您可以覆盖def destroy()
函数。在其中获取对象并更改is_deleted = True
的状态 例如:
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
instance.is_deleted = True
instance.save()
return Response(status=status.HTTP_204_NO_CONTENT)
对于你的第二个问题,我建议为 django 编写一个显式过滤器,它使用filters.BaseFilterBackend
例如:
import django_filters
from rest_framework import filters
class TrackFilterBackend(filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
return queryset.filter(is_deleted=False)
然后将其作为filter_class = TrackFilterBackend
添加到您的视图中。
您可以找到有关DJango REST过滤的更多详细信息
您也可以使用SerializerMethodField
:
class AlbumSerializer(serializers.ModelSerializer):
tracks = SerializerMethodField()
def get_tracks(self, album):
tracks = album.track_set.filter(is_deleted=False)
return TrackSerializer(tracks, many=True).data