ValueError:字段“ id”应为数字,但为“ super”。 [04 / Apr / 2020 18:15:11]“ GET / super_user HTTP / 1.1” 500 132224

时间:2020-04-04 15:28:30

标签: python django python-3.x django-models

我的项目使用户可以写帖子,每个帖子都有一个类别,每个类别都有一个主管,所以我遇到一个问题,我无法获得特定主管的特定类别的所有帖子。

我的模型

class PostCategory(models.Model):
    name = models.CharField(max_length=150, null=True)
    description = models.CharField(max_length=100, null=True)

    def __str__(self):
        return self.name

class UserSupervisorSpecialist(models.Model):
    user = models.ForeignKey(User, related_name="user_id", on_delete=models.SET_NULL, null=True)
    category = models.OneToOneField(CategoryPost, related_name="category_id", on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.user.username

class Post(models.Model):
    title = models.CharField(max_length=100, null=True)
    content = RichTextField()
    post_category = models.ManyToManyField(CategoryPost)
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    date_create = models.DateTimeField(auto_now_add=True, null=True)
    status = models.BooleanField(default=False) #0 -> suspend
    likes = models.IntegerField(default=0) #0 -> No like yet
    supervisor = models.ForeignKey(UserSupervisorSpecialist, on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return self.title

    def snippet(self):
        return self.content[:150]

我的观点

def control_spanle(request):
    category = UserSupervisorSpecialist.objects.filter(user=request.user)
    post_filter = Post.objects.filter(supervisor=request.user.username)
    return render(request, 'supervisor/controle_panel.html',{'Category':category,'post_filter':post_filter})

1 个答案:

答案 0 :(得分:2)

您在Post模型上写了一个查询集,但未指定字段,而将其与字符串进行比较。

您可以通过更改此行来解决此问题

post_filter = Post.objects.filter(supervisor=request.user.username)

收件人:

post_filter = Post.objects.filter(supervisor__name=request.user.username)