我是django的新手,我目前正在使用djangorestframework开展一个小项目。我在创建活动流时遇到了问题,但我偶然发现了getstream.io(stream-django)并且我无法绕过它。我想看一个如何使用DRF和stream-django创建活动流的示例。
我只是放弃了我正在使用的模型和序列化器。非常感谢。
Models.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)
serializers.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']
views.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)