Hi everybody.

I'm new here and a little less using DRF. I've been looking for an answer 
before asking here. Hope you can help me.

I have this scenario:

class Job(models.Model):
    owner = models.ForeignKey('auth.User', related_name='jobs')
    datafiles = models.ManyToManyField(DataFile, blank=True)

class DataFile(models.Model):
    owner = models.ForeignKey('auth.User', on_delete=models.CASCADE, 
related_name='datafiles')
    file = models.FileField(upload_to=user_data_directory_path, blank=False)

class DataFileViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        return self.request.user.datafiles.all()

class JobSerializer(serializers.ModelSerializer):
    class Meta:
        model = Job
        fields = (
            'datafiles',

In /api/datafiles i get datafiles created by owner but in serializer, when 
i try to create a new job, the serializer provides me all the available 
datafiles (being owner or not).

I've tried adding datafiles field in serializer like this (it doesn't work):

datafiles = serializers.PrimaryKeyRelatedField(queryset=DataFile.objects.
filter(owner=owner), read_only=True)

If i write something like "owner=2" (2 is my user_id) i get what i want but 
the id should be got dinamically and i'm not able to do it. Not sure if i'm 
following the good path.

Thank you for your time. Regards!

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to