按主键过滤查询集

Query set filtering by Primary Key

我有一个网站,最初旨在显示数据表中动态添加或删除的模型。现在数据增加了,我添加了服务器端处理。问题是我为每个外键都有一个自定义页面来显示属于它自己的数据现在我被困在如何过滤外键 ID 的查询集并为服务器端处理设置 api url .我希望我很清楚。感谢您的关注

class Process(models.Model):
    
    user = models.ForeignKey(User, related_name="user", on_delete=models.CASCADE)
    entry_date = models.DateField(default=datetime.date.today)
    product = models.CharField(max_length=77)

在观点中我有一个观点:

class UserViewSet(viewsets.ModelViewSet):

    serializer_class = ProcessSerializer
    def get_queryset(self):
        pk = self.request.parser_context['kwargs']['pk']
        queryset = Process.objects.filter(user__pk=pk)
        return queryset

Api url 中的 returns 页:

http://127.0.0.1:8000/api/user_processes/1/?format=datatables

{"data":{"id":1,"user":{"id":1,"name":"tealy","phone":null,"email":null,"date_created":"2021-10-17T21:15:42.131996+03:00"},"entry_date":"2021-10-17","user":1, "product":null},"recordsFiltered":6354,"recordsTotal":6354,"draw":1}

这意味着我有属于该用户的所有进程的计数,但我无法获取进程的信息。

网址:

path("user_processes/<int:pk>/", views.user_processes, name="user_processes")
router.register(r'user_processes', views.UserViewSet, basename='UserProcess')

views.py:

 class UserView(generics.ListAPIView):

    serializer_class = ProcessSerializer
    def get_queryset(self):
        pk = self.request.parser_context['kwargs']['id']
        user = User.objects.get(pk=pk)
        queryset = Process.objects.filter(user=user)
        return queryset

urls.py(项目):

path('api/users/<int:id>/', views.UserView.as_view(), name='users'),

urls.py(应用程序):

path("users/<int:pk>/", views.users, name="users"),

views.py:

def users(request, pk):
    return render(request, 'tealyapp/temp.html', {'pk': pk})