在Django中创建条件Many to Many关系

huangapple go评论127阅读模式
英文:

Creating a conditional Many to Many relation in Django

问题

看起来很容易,但我仍然不知道如何实现它。

尝试在三个模型之间创建多对多的条件关系(不确定是否是正确的命名方式!):


Class Book(models.Model):
    book_name = models.CharField(max_length=50)


Class Article(models.Model):
    article_name = models.CharField(max_length=50)


Class Portfolio(models.Model):
    KINDS = [('B', 'Book'), ('A', 'Article')]
    book = models.ForeignKey(Book, on_delete=models.PROTECT)
    article = models.ForeignKey(Article, on_delete=models.PROTECT)
    kind = models.models.CharField(max_length=1, choices=KINDS)
    user = models.ManyToManyField(User, through='Recommendation')


Class Recommendation(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    portfolio = models.ForeignKey(Book, on_delete=models.PROTECT)
    date = models.DateTimeField()
英文:

It seems easy but I still can't figure out how to implement it.

Trying to create a Many to Many conditional relations (not sure if it's the correct way to name it!) between three models:


Class Book(models.Model):
    book_name = models.CharField(max_length=50)


Class Article(models.Model):
    article_name = models.CharField(max_length=50)


Class Portfolio(models.Model):
    KINDS = [('B', 'Book'), ('A', 'Article')]
    book = models.ForeignKey(Book, on_delete=models.PROTECT)
    article = models.ForeignKey(Article, on_delete=models.PROTECT)
    kind = models.models.CharField(max_length=1, choices=KINDS)
    user = models.ManyToManyField(User, through='Recommendation')


Class Recommendation(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    portfolio = models.ForeignKey(Book, on_delete=models.PROTECT)
    date = models.DateTimeField()

Each User (from the Django User module) can have multiple recommendations of portfolios. Each Portfolio is a group of books and articles recommended on a specific date.

答案1

得分: 2

我会跳过 Portfolio Model,直接这样实现:

class Book(models.Model):
    book_name = models.CharField(max_length=50)


class Article(models.Model):
    article_name = models.CharField(max_length=50)


class Recommendation(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    book = models.ForeignKey(
        Book,
        blank=True,
        null=True,
        on_delete=models.PROTECT
    )
    article = models.ForeignKey(
        Article,
        blank=True,
        null=True,
        on_delete=models.PROTECT
    )
    date = models.DateTimeField()

    class Meta:
        constraints = [
            models.CheckConstraint(
                check=Q(book__isnull=True, article__isnull=False) | Q(book__isnull=False, article__isnull=True),
                name='only_book_or_article'
            )
        ]

你可以跳过约束,但这样可能会导致你的数据不一致。

英文:

I would skip the Portfolio Model and just implement it this way:

class Book(models.Model):
    book_name = models.CharField(max_length=50)


class Article(models.Model):
    article_name = models.CharField(max_length=50)


class Recommendation(models.Model):
    user = models.ForeignKey(User, on_delete=models.PROTECT)
    book = models.ForeignKey(
        Book,
        blank=True,
        null=True,
        on_delete=models.PROTECT
    )
    article = models.ForeignKey(
        Article,
        blank=True,
        null=True,
        on_delete=models.PROTECT
    )
    date = models.DateTimeField()

    class Meta:
        constraints = [
            models.CheckConstraint(
                check=Q(book__isnull=True, article__isnull=False) | Q(book__isnull=False, article__isnull=True),
                name='only_book_or_article'
            )
        ]

you could skip the constrains but than you risk that your data is inconsistent

huangapple
  • 本文由 发表于 2020年1月7日 02:08:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定