Add multiple "Place of Interest" model to a "Tour" model in Django and allow one "Place of Interest" to be in multiple "Tour" model

Viewed 31

I want to create a tour app that consists of "Tour" and "Place of Interest" The "Tour" model would have multiple "Place of Interests". One "Place of Interest" should be able to be added to multiple "Tour".

So far, I can only add one "Place of Interest" to one "Tour".

Here's my model:

class PlaceOfInterest(models.Model):
    name = models.CharField(max_length=255)
    tour = models.ForeignKey("Tour", on_delete=models.CASCADE, related_name="tours", blank=True, null=True)

class Tour(models.Model):
    title = models.CharField(max_length=255)

What is the best way to represent this situation?

1 Answers

To solve this you can define a Many-to-many relationship form Tour to PlaceOfInterest

Give this a try

class PlaceOfInterest(models.Model):
    name = models.CharField(max_length=255)

class Tour(models.Model):
    title = models.CharField(max_length=255)
    tour = models.ManyToManyField("PlaceOfInterest", related_name="tour_poi", blank=True, null=True)
Related