英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论