[英]ForeignKey model query filter
Using filter(), how do I get the foreign key property 'recipient' of the current authenticated user ? 使用filter(),如何获取当前已认证用户的外键属性“收件人”?
Models.py: Models.py:
class Message(models.Model):
recipient = models.ForeignKey(CustomUser, on_delete = models.CASCADE,related_name = 'recipient',null = True)
sender = models.ManyToManyField(CustomUser,related_name = 'messages')
date = models.DateTimeField(auto_now_add=True, blank=True)
subject = models.CharField(max_length = 1000, blank = True)
message = models.TextField(blank=True, null=True)
unread = models.BooleanField(default = True)
class CustomUser(User):
user_bio = models.TextField(blank=True, null=True)
birth_date = models.DateField(null=True, blank=True)
def __str__(self):
return self.username
Views.py: Views.py:
### Inbox list class
class InboxListView(ListView):
'''
This view lets the user view all the messages created in a list
'''
model = Message# [Message,SafeTransaction] # I want to be able to draw from two models/objects #
template_name = "myInbox/inbox.html"
paginate_by = 5
def get_context_data(self, **kwargs):
context = super(InboxListView, self).get_context_data(**kwargs)
context['message_list'] = Message.objects.filter(recipient=CustomUser.SOMETHING_idk)#INCORRECT FILTRATION, FIX ASAP!!!!!!!!!!!!!#
context['now'] = timezone.now()
context['safeTransaction_list'] = SafeTransaction.objects.all()
return context
Specifically this line is what I need puzzled out : 具体来说,这是我需要解决的问题:
context['message_list']=Message.objects.filter(recipient=CustomUser.SOMETHING_idk)
What can I put as the filter parameter for a specific message recipient ? 我可以将什么作为特定邮件收件人的过滤器参数? I tried something along the lines of CustomUser or CustomUser.pk, or request.authenticated_as_the_thing_I_want_specifically.
我尝试了类似CustomUser或CustomUser.pk或request.authenticated_as_the_thing_I_want_specific的方法。 etcetera.
等。
I seem to be a bit lost with it. 我似乎有点迷失了。
any help at all is appreciated. 任何帮助都表示赞赏。
It looks like you've created a custom auth user model? 看来您已经创建了自定义auth用户模型? Assuming that's been setup correctly you should be able to do the following:
假设设置正确,您应该可以执行以下操作:
def get_context_data(self, **kwargs):
context = super(InboxListView, self).get_context_data(**kwargs)
context.update({
'message_list': Message.objects.filter(recipient=self.request.user),
'now': timezone.now(),
'safeTransactionList': SafeTransaction.objects.all(),
})
return context
Some additional things of note: 一些其他注意事项:
now
is available as a django template tag. now
可以作为django模板标签使用。 You can {% now %}
in your template code. {% now %}
。 {% for message in request.user.recipient.all %}
or {% for message in request.user.messages.all %}
instead of making the ORM call in get_context_data
to iterate over message_list
for the logged in user. {% for message in request.user.recipient.all %}
或{% for message in request.user.messages.all %}
在get_context_data
中进行ORM调用以遍历message_list
为已登录用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.