class User(models.Model):
username = models.CharField(max_length=30)
email = models.CharField(max_length=60, primary_key=True)
registration_date = models.DateField(default=datetime.date.today)
class Task(models.Model):
author = models.ForeignKey(
User, on_delete=models.PROTECT,
related_name="%(app_label)s_%(class)s_author"
)
likes = models.ManyToManyField(
User,
related_name="%(app_label)s_%(class)s_like"
)
dislikes = models.ManyToManyField(
User,
related_name="%(app_label)s_%(class)s_dislike"
)
subscribed_users = models.ManyToManyField(User)
我想为User创建一个方法,它返回该用户喜欢或不喜欢的所有任务。我查看了django orm的文档,并且没有找到我的能力,给定一个User实例,找到他喜欢的所有任务。
答案 0 :(得分:1)
您只需要使用related_name,如:
some_user.app_label_class_like.all()