使用 select_related 作为逗号分隔的字段

Use select_related for comma separated field

我有两个这样的 class 模型:

class Problem(models.Model):
    owner = models.ForeignKey(User)
    services = models.CommaSeparatedIntegerField(max_length=200)

class Services(models.Model):
    name = models.CharField(max_length=50)
    ...

如您在问题模型中所见,我将服务的 ID 保留在逗号分隔的字段中。例如要解决这个问题,我们应该做这个服务:“1,2,3”。我想向用户显示问题和相关服务的详细信息。为此,我应该将问题加入服务 table,但我不知道如何加入。考虑到我的超级管理员定义的服务数量有限。例如我有 150 项服务。

使用包含 ForeignKey 的服务。因此,您可以将所有服务关联到一个问题

使用 ManyToManyField,这正是它们的用途。