代码之家  ›  专栏  ›  技术社区  ›  Ben2pop

正在检索要在查询中使用的ID,Django

  •  0
  • Ben2pop  · 技术社区  · 6 年前

    我想我有一个简单的问题:

    我试图查询我的数据库,以检索链接到特定团队、链接到特定项目的团队成员的数量。

    team_member_count = Project.objects.get(id = id).team_id.members.count()
    

    该查询使用以下url来查询项目详细信息视图: localhost/website/project/141/ 在shell中,我的查询是Project。物体。get(id=141)。团队成员。计数()

    但从我的观点来看:

    TypeError: int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
    

    我的观点如下(检索chart.js的数据)

    class ChartData(APIView):
    
        authentication_classes = []
        permission_classes = []
    
        def get(self, request, format=None, *args, **kwargs):
            user_count = MyUser.objects.all().count()
            project_count = Project.objects.all().count()
            team_member_count = Project.objects.get(id = id).team_id.members.count()
            labels = ["Users", "Projects", "Team_number", "Green", "Purple", "Orange"]
            default_items = [user_count, project_count,team_member_count,28,12,32]
            data = {
                "labels":labels,
                "default":default_items,
            }
            return Response(data)
    

    模型:

    class Team(models.Model):
        team_name = models.CharField(max_length=100, default = '')
        team_hr_admin = models.ForeignKey(MyUser, blank=True, null=True)
        members = models.ManyToManyField(MyUser, related_name="members")
    
        def __str__(self):
            return self.team_name
    
    
    class Project(models.Model):
        name = models.CharField(max_length=250)
        team_id = models.ForeignKey(Team, blank=True, null=True)
        project_hr_admin = models.ForeignKey('registration.MyUser', blank=True, null=True)
        candidat_answers = models.ManyToManyField('survey.response')
    
        def get_absolute_url(self):
            return reverse('website:ProjectDetails', kwargs = {'pk' : self.pk})
    
        def __str__(self):
            return self.name
    

    URL模式:

    app_name = 'website'
    urlpatterns = [
        url(r'^hr_index/$', views.HRIndex.as_view(), name='hr_index'),
        url(r'^candidate_index/$', views.CandidateIndex.as_view(),name='candidate_index'),
        url(r'^employee_index/$', views.EmployeeIndex.as_view(),name='employee_index'),
        url(r'^addproject/$', views.ProjectCreate.as_view(), name='add_project'),
        url(r'^addteam/$', views.TeamCreate.as_view(), name='add_team'),
        url(r'^linkteam/$', views.LinkTeam.as_view(), name='link_team'),
        url(r'^linkteam2/$', views.TeamSelect, name='team_select'),
        url(r'^project/(?P<pk>[0-9]+)/$',views.ProjectDetailView.as_view(), name='ProjectDetails'),
        url(r'^project/(?P<pk>[0-9]+)/api/chart/data/$', views.ChartData.as_view(), name='chartdata'),
    
    ]
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Daniel Roseman    6 年前

    你没有一个局部变量叫做 id 在你的 get 方法Python假设您指的是全局内置 id() 函数,从而产生错误。

    根据您的URL,您正在捕获一个名为 pk . 所以你应该从 kwargs 词典:

    Project.objects.get(id=kwargs['pk'])...