0

I have this model from this question:

class Category(models.Model):
    category = models.CharField(max_length=50)

    def __str__(self):
        return self.category

class Tag(models.Model):
    tag = models.CharField(max_length=50)

    def __str__(self):
        return self.tag

class Video(models.Model):
    title = models.CharField(max_length=255)
    categories = models.ManyToManyField(Category, through='Taxonomy', through_fields=('video', 'category'))
    tags = models.ManyToManyField(Tag, through='Taxonomy', through_fields=('video', 'tag'))

    def __str__(self):
        return self.title

class Taxonomy(models.Model):
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True)
    tag = models.ForeignKey(Tag, on_delete=models.CASCADE, null=True)
    video = models.ForeignKey(Video, on_delete=models.CASCADE)

How should I implement a form that let me create a new video, with their associated categories and tags using these models bearing it mind it has and intermediary table using through_fields ?

NOTE: Use edit history (revisions) to see the previous questions before I reworded It.

Jeflopo
  • 2,192
  • 4
  • 34
  • 46
  • The definition of a through model is that you create a row for every relationship between the two models you're linking - i.e., you have to create a `Taxonomy` instance for every cat/tag that is linked to a video. If you don't want to do that, then a through model isn't the right approach. I am not sure whether you need your `Taxonomy` model at all - why can't you just have m2m relationships between video/tag and video/category? – solarissmoke Mar 30 '18 at 11:17
  • I wanted to have a custom intermediary table. For future needs. – Jeflopo Mar 30 '18 at 11:25
  • In which case you have to create one `Taxonomy` object for each relationship that you want to save. – solarissmoke Mar 30 '18 at 11:25
  • In the shell it forces me to create an instance for each relationship, I see it now. But using forms too ? Couldn't I pass a list of arguments and django handle that ? Maybe I should reword my question On how to implement this using an add form. – Jeflopo Mar 30 '18 at 11:31
  • I have reworded the whole question. – Jeflopo Mar 30 '18 at 11:46

0 Answers0