我正在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())