Django表上的Django rest框架过滤器

时间:2019-06-06 06:03:10

标签: django django-rest-framework django-filter

我正在django rest框架中编写API,我想对外键表进行过滤。

想根据用户在scripts_details表中进行过滤。由于scripts_details没有用户,因此我尝试在脚本表中进行过滤。却没有发生。

我很累return ScriptDetails.objects.filter(script=scriptid, script.user=self.request.user)

Models.py

class Scripts(models.Model):
    tutorial = models.OneToOneField(TutorialDetail)
    language = models.ForeignKey(Language)
    status = models.BooleanField(default=False)
    data_file = models.FileField(upload_to='scripts',blank=True)
    user = models.ForeignKey(User,related_name='user_id')


class ScriptDetails(models.Model):
    cue=models.TextField()
    narration=models.TextField()
    order=models.PositiveIntegerField()
    script=models.ForeignKey('Scripts', on_delete = models.CASCADE)

serializer.py

class ScriptsDetailSerializer(serializers.ModelSerializer):

  class Meta:
    model=ScriptDetails
    fields=('cue','narration','order','script')

views.py


class ScriptsDetailsList(generics.ListCreateAPIView):
    serializer_class=ScriptsDetailSerializer

    def get_queryset(self):
      scriptid = self.kwargs.get('scriptid')
      return ScriptDetails.objects.filter(script=scriptid)

urls.py

url(r'^api/scriptsdetails/(?P<scriptid>[0-9]+)/', views.ScriptsDetailsList.as_view())

0 个答案:

没有答案