How to implement a form to add a video using M2M model with through_fields?

点点圈 提交于 2019-12-11 16:22:16

问题


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.

来源:https://stackoverflow.com/questions/49573288/how-to-implement-a-form-to-add-a-video-using-m2m-model-with-through-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!