0

I want to create a udemy like web app and using django rest framework for the backend and mysql as database. i have a model named 'Lessons' that contains list of all lessons and one of the fields is 'video-link'. also i have another model names Purchases that have two ForeignKey fields :Users and Lessons. i want to show the Lessons to all users but for the download field i have to lookup the pair (User , Lesson) in Purchases and if He has the course i will show him the download field.

My View Set

class LessonsViewSet(viewsets.ModelViewSet):
    queryset = models.Assignments.objects.all()
    authentication_classes = (TokenAuthentication,)

    def get_serializer_class(self):
        if self.request.user.is_staff :
            print(self.request.user)
            return serializers.FullAccessLessonsSerializer
        elif self.request.user.is_active:
            return serializers.PartialAccessLessonsSerializer
        print(self.request.user)
        return serializers.BasicAccessLessonsSerializer

My Serializers

Full access for admins:

class FullAccessLessonsSerializer(serializers.ModelSerializer):

    class Meta:
        model = models.Assignments
        fields = ('id', 'title', 'description', 'dllink' )

Basic access for unauthenticated users:

class BasicAccessLessonsSerializer(serializers.ModelSerializer):

    class Meta:
            model = models.Assignments
            fields = ('id', 'title', 'description')

and partial access for students :

class PartialAccessAssignmentsSerializer(serializers.ModelSerializer):
    """A serializer for all Lessons"""

    def __init__(self, *args, **kwargs):
        fields = kwargs.pop('fields', None)
        super(PartialAccessAssignmentsSerializer, self).__init__(*args,**kwargs)
        print(self.fields.get('id'))
        self.fields.pop('dllink')

    class Meta:
        model = models.Assignments
        fields = ('id','title','description','dllink' )

I have done anything I could found but I can not figure it out. I either an get error or removing all dl links.

4
  • So do you want to return dllink conditionally depending on whether or not the requesting user has already downloaded the Assignments? Commented Sep 10, 2018 at 0:48
  • it is bit confusing. You talk about models Lessons And Purchases but in your code, you use only model Assignments. So it is not clear what fields from where you want to show and under which conditions. I'd recommend to make it bit clearer and include models as well. Commented Sep 10, 2018 at 7:43
  • @HenryWoody I got my answer from Ykh . thank you . No I wanted to return dllink conditionally depending on whether or not the requesting user has already purchased the Course . Commented Sep 11, 2018 at 8:44
  • @EnthusiastMartin . yeah sorry about that . i wanted to make it simple . i posted just a few lines of code . perhaps i had to post my models too. it was my first question in stackoverflow . thank you for your comment. Commented Sep 11, 2018 at 12:38

1 Answer 1

1

If you want dllink is None when user can't access.use this:

class FullAccessLessonsSerializer(serializers.ModelSerializer):
    dllink = serializers.SerializerMethodField()

    def get_prescription_accept(self, instance):
        result =  True # lookup the pair (User , Lesson) in Purchases
        if result:
           return instance.dllink
        else:
           return ''

    class Meta:
        model = models.Assignments
        fields = ('id', 'title', 'description', 'dllink' )

If you want pop dllink from data,use:

class FullAccessLessonsSerializer(serializers.ModelSerializer):

    def to_representation(self, instance):
        data = super(FullAccessLessonsSerializer, self).to_representation(instance)
        result =  True # lookup the pair (User , Lesson) in Purchases
        if not result:
            data.pop('dllink')
        return data

    class Meta:
        model = models.Assignments
        fields = ('id', 'title', 'description', 'dllink' )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.