是否可以在下面的“详细信息”视图中将模型中的布尔值设置为false?
我在django文档(查询集)中查找了类似.set()
方法的内容,它似乎存在,但不适用于这种特殊情况。
如何通过视图切换unread
中的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)
Views.py:
### Message detail class
class MessageInboxDetail(DetailView):
'''
This view lets the user view the details of a message created
'''
context_object_name = 'message_detail'
model = Message
template_name = "myInbox/message_detail.html"
def get_context_data(self, **kwargs):
context = super(MessageInboxDetail, self).get_context_data(**kwargs)
context.update({
'message_detail': Message.unread.set(False) }) # Message(unread=True/False)
return context
答案 0 :(得分:2)
您没有通过.set(..)
调用来设置模型实例的字段,而是通过分配给该属性(在幕后,Django修补了__get__
和__set__
函数)
因此,我们可以通过从上下文中检索message
对象(使用context['message_detail']
,然后更改其状态,最后将更新的版本保存到数据库中来获取class MessageInboxDetail(DetailView):
'''
This view lets the user view the details of a message created
'''
context_object_name = 'message_detail'
model = Message
template_name = "myInbox/message_detail.html"
def get_context_data(self, **kwargs):
context = super(MessageInboxDetail, self).get_context_data(**kwargs)
message = context['message_detail']
message.unread = False
message.save()
return context
对象。例如:>
<table cellspacing="0" cellpadding="0" border="0" style="width: 390px; height: 70px; table-layout: fixed;">
<tbody>
<tr>
<td rowspan="4" style="width: 72px; height: 72px; padding-right: 0px; overflow: auto;" vertical-align:"middle" valign="middle">
<a href="http://google.com.au"><img id="TemplateLogo" data-class="external" src="https://dummyimage.com/70.png" alt="Company Name" style="display: inline-block; margin-left: auto; margin-right: auto; vertical-align: baseline;" height="100%" width="100%"></a>
</td>
<td style="vertical-align: middle; padding-left: 10px; width: 264px; height: 18px;">
<font style="font-weight: bold; font-family: Arial; font-size: 12pt; color: rgb(81, 81, 81);">
John Doe
</font>
</td>
</tr>
<tr>
<td style="padding-bottom: 0px; vertical-align: middle; padding-left: 10px; width: 264px; height: 18px;">
<font style="font-family: Arial; font-size: 10pt; color: rgb(81, 81, 81);">
Accounts
</font>
</td>
</tr>
<tr>
<td style="vertical-align: middle; padding-left: 10px; width: 264px; height: 18px;">
<font style="color: #515151; font-size: 10pt; font-family: Arial">
T: <a href="tel:+6199999999" style="color: #7cc0cb; text-decoration: none; border-bottom: 0px solid #7cc0cb;">(02) 4399 9999</a> |
F: <a href="tel:+6199999999" style="color: #7cc0cb; text-decoration: none; border-bottom: 0px solid #7cc0cb;">(02) 4399 9999</a>
</font>
</td>
</tr>
<tr>
<td style="vertical-align: middle; padding-left: 10px; width: 264px; height: 18px;">
<font style="color: #515151; font-size: 10pt; font-family: Arial">
E: <a href="mailto:example@example.com.au" style="color: #7cc0cb; text-decoration: none; border-bottom: 0px solid #7cc0cb;">example@example.com.au</a>
</font>
</td>
</tr>
</tbody>
</table>
但是我不知道在详细视图中将消息标记为已读是否是一个好主意,它可能导致代码重复,但这通常不是一个好主意。