Django - 在视图中通过外键查询
Django - Query by foreign key in views
我正在尝试在 post 上制作一个按钮,当用户点击它时,请求将其作为出席添加到 post,然后 post 必须批准该请求。
Models.py
class Attending(models.Model):
is_approved = models.BooleanField(default=False)
attending = models.ManyToManyField(User, related_name='user_event_attending')
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField(blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
attending = models.ForeignKey(Attending, on_delete=models.CASCADE, verbose_name='atending', null=True)
我的问题是,每次我为按钮编写查询时都会出现错误,我无法弄清楚如何获取外键的反向。
这是我在 views.py
上的代码
def request_event(request, pk):
previous = request.META.get('HTTP_REFERER')
try:
query = Attending.objects.get(pk=pk)
request_attending = query.post_set.add(request.user)
messages.success(request, f'Request sent!')
return redirect(previous)
except query.DoesNotExist:
return redirect('/')
非常感谢您的提前帮助!
这:query.post_set
只是关系。你不能像那样调用方法 add
。您可以添加到 ManyToMany
关系,我相信您想将用户添加到 Attending.attending
字段,而不是直接添加到 Post
对象。将其更改为:
...
query = Attending.objects.get(pk=pk)
query.attending.add(request.user)
messages.success(request, f'Request sent!')
....
|更新 |
我认为你应该考虑重新安排你的人际关系。如果我理解你的计划,你应该这样做:
class Attending(models.Model):
...
attendant = models.ForeignKey(User, related_name='events_attending', on_delete=models.CASCADE)
post = models.ForeignKey('Post', on_delete=models.CASCADE)
class Post(models.Model):
...
author = models.ForeignKey(User, on_delete=models.CASCADE)
一个Post
对象可以有多个Attending
对象,那么你可以使用这样的关系:
att = Attending.objects.first()
att.post # get related Post object from ForeignKey | there is only one
post = Post.objects.first()
post.attending_set.all() # get all related Attending objects
Post.objects.get(attending=att) # get Post object that the Attending object have in ForeignKey field
user = User.objects.first()
user.post_set.all() # get all Post objects that User is author in
user.events_attending.all() # get all related Attending objects
更多检查Django Docs。
我正在尝试在 post 上制作一个按钮,当用户点击它时,请求将其作为出席添加到 post,然后 post 必须批准该请求。
Models.py
class Attending(models.Model):
is_approved = models.BooleanField(default=False)
attending = models.ManyToManyField(User, related_name='user_event_attending')
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField(blank=True)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
attending = models.ForeignKey(Attending, on_delete=models.CASCADE, verbose_name='atending', null=True)
我的问题是,每次我为按钮编写查询时都会出现错误,我无法弄清楚如何获取外键的反向。
这是我在 views.py
上的代码def request_event(request, pk):
previous = request.META.get('HTTP_REFERER')
try:
query = Attending.objects.get(pk=pk)
request_attending = query.post_set.add(request.user)
messages.success(request, f'Request sent!')
return redirect(previous)
except query.DoesNotExist:
return redirect('/')
非常感谢您的提前帮助!
这:query.post_set
只是关系。你不能像那样调用方法 add
。您可以添加到 ManyToMany
关系,我相信您想将用户添加到 Attending.attending
字段,而不是直接添加到 Post
对象。将其更改为:
...
query = Attending.objects.get(pk=pk)
query.attending.add(request.user)
messages.success(request, f'Request sent!')
....
|更新 |
我认为你应该考虑重新安排你的人际关系。如果我理解你的计划,你应该这样做:
class Attending(models.Model):
...
attendant = models.ForeignKey(User, related_name='events_attending', on_delete=models.CASCADE)
post = models.ForeignKey('Post', on_delete=models.CASCADE)
class Post(models.Model):
...
author = models.ForeignKey(User, on_delete=models.CASCADE)
一个Post
对象可以有多个Attending
对象,那么你可以使用这样的关系:
att = Attending.objects.first()
att.post # get related Post object from ForeignKey | there is only one
post = Post.objects.first()
post.attending_set.all() # get all related Attending objects
Post.objects.get(attending=att) # get Post object that the Attending object have in ForeignKey field
user = User.objects.first()
user.post_set.all() # get all Post objects that User is author in
user.events_attending.all() # get all related Attending objects
更多检查Django Docs。