Search code examples
pythondjangocsvdjango-select-related

Use select_related for comma separated field


I have two class model like this:

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

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

As you see in Problem model I keep id's of Services in comma separated field. For example to solve this problem we should do this Services: "1,2,3". I want to show detail of problem and related services to user. To do this I should join Problem to Services table but I don't know how. Consider that I have limited number of Services that defined by super admin. for example I have 150 services.


Solution

  • Use a ManyToManyField, this is exactly what they are used for.