我是 django 的新手,我目前正在使用 djangorestframework 进行一个小项目。我在创建活动流时遇到了问题,但我偶然发现了 getstream.io(stream-django),但我一直无法理解它。我想看一个如何使用 DRF 和 stream-django 创建活动流的示例。
我将删除我正在使用的模型和序列化程序。非常感谢。
模型.py
class Imprest(models.Model, Activity):
user = models.ForeignKey(User, on_delete=models.CASCADE)
description = models.TextField()
amount = models.PositiveIntegerField()
is_approved = models.BooleanField(default=False)
created_at = models.DateTimeField(default=datetime.now)
def __str__(self):
return 'Raised by: {}'.format(self.user)
序列化程序.py
class ImprestSerializer(serializers.ModelSerializer):
user = UserSerializer(read_only=True, required=False)
class Meta:
model = Imprest
fields = '__all__'
def get_validation_exclusions(self, instance=None):
exclusions = super(ImprestSerializer, self).get_validation_exclusions(instance)
return exclusions + ['user']
视图.py
class ImprestViewSet(ModelViewSet):
queryset = Imprest.objects.all()
serializer_class = ImprestSerializer
permission_classes = permissions.IsAuthenticated,
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return permissions.AllowAny(),
return permissions.IsAuthenticated(),
def perform_create(self, serializer):
instance = serializer.save(raised_by=self.request.user)