如何实现表单添加使用M2M模型through_fields视频?(How to implement

2019-10-28 15:18发布

我从这个模型这个问题 :

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)

我应该如何实现,让我创建一个新的视频形式,与他们相关的类别和使用这些型号轴承它介意它的标签和使用中介表through_fields

:使用编辑历史(修订)看前面的问题之前,我改写它。

文章来源: How to implement a form to add a video using M2M model with through_fields?