How to use stream-django with djangorestframework

298 views Asked by At

I'm new to django and I'm currently working on a small project using djangorestframework. I was having issues creating an activity stream but I stumbled on getstream.io(stream-django) and I've been unable to wrap my head around it. I'll like to see a example of how an activity stream can be created using DRF and stream-django.

I'll just drop the model and serializer i'm working with. Many thanks.

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)
0

There are 0 answers