- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个简单的聊天应用程序。这是我的模型。
class Thread(models.Model):
participants = models.ManyToManyField(User, related_name= 'threads_partof')
last_message_time = models.DateTimeField(null=True, blank=True)
class Message(models.Model):
message = models.CharField(max_length=500)
sender = models.ForeignKey(User)
thread = models.ForeignKey(Thread, related_name = 'thread_to_message')
datetime = models.DateTimeField(auto_now_add=True)
def update_last_message_datetime(sender, instance, created, **kwargs):
'''
Update Thread's last_message field when
a new message is sent.
'''
if not created:
return
Thread.objects.filter(id=instance.thread.id).update(last_message_time=instance.datetime)
post_save.connect(update_last_message_datetime, sender=Message)
我使用 Django-Tastypie 作为我的 API。这是我的应用程序当前工作方式的示例,如果史蒂夫想要向比尔发送消息,则会创建一个新的“消息”对象,其中史蒂夫是发送者。当创建消息对象时,它也会创建一个“Thread”对象。在这个帖子中,参与者是 Steve 和 Bill。
我当前有一个 ThreadPreview 资源。
class ThreadPreview(ModelResource):
participants = fields.ManyToManyField(BasicFoodieActivityResource, 'participants', full=True)
class Meta:
queryset = Thread.objects.all()
resource_name = 'thread-preview'
fields = ['id', 'participants', 'last_message_time']
authorization = Authorization()
authentication = BasicAuthentication()
serializer = Serializer(formats=['json'])
include_resource_uri = False
always_return_data = True
def dehydrate(self, bundle):
bundle.data['last_message'] = bundle.obj.thread_to_message.latest('datetime').message
return bundle
def get_object_list(self, request):
return super(ThreadPreview, self).get_object_list(request).filter(participants = request.user).order_by('-last_message_time')
当我对此资源执行 HTTP GET 请求时,我得到的结果是:
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [{
"id": "12",
"last_message": "Hey, Bill, you're stealing from us!",
"last_message_time": "2013-11-07T19:27:52",
"participants": [{
"user": {
"first_name": "Steve",
"id": "28",
"last_name": "Jobs",
"username": "steve"
},
"userPic": "http://apple.com/stevejobs.jpg"
}, {
"user": {
"first_name": "Bill",
"id": "6",
"last_name": "Gates",
"username": "bill"
},
"userPic": "http://microsoft.com/billgates.jpg"
}],
}]
}
当数据返回时,我不想包括用户 Steve,因为他是 request.user。每当 request.user 访问此数据时,我都希望他们的用户数据不被序列化,因为我已经知道他们是谁。例如,这就是我想要的:
{
"meta": {
"limit": 20,
"next": null,
"offset": 0,
"previous": null,
"total_count": 1
},
"objects": [{
"id": "12",
"last_message": "Hey, Bill, you're stealing from us!",
"last_message_time": "2013-11-07T19:27:52",
"participants": [{
"user": {
"first_name": "Bill",
"id": "6",
"last_name": "Gates",
"username": "bill"
},
"userPic": "http://microsoft.com/billgates.jpg"
}],
}]
}
我该如何执行此操作,以便始终忽略 request.user 的数据?
最佳答案
您可以在 ThreadPreview
上使用 deHydrate_participants
钩子(Hook) ( see docs on dehydration ) 来过滤参与者:
class ThreadPreview(ModelResource):
...
def dehydrate_participants(self, bundle):
if not hasattr(bundle.request, 'user'):
return bundle.data['participants']
new_participants = []
for p in bundle.data['participants']:
if p['user']['id'] != bundle.request.user.pk:
new_participants.append(p)
return new_participants
...
注意:我对p['user']['id'] !=bundle.request.user.pk
的理解可能是错误的。 p['user']['id']
可能已经是字符串。
Resource
有一个钩子(Hook)方法 def get_object_list(self, request)
它可用于根据 request.user
过滤列表。实现是战略性的
class BasicFoodieActivityResource(ModelResource):
...
def get_object_list(self, request):
qs = super(BasicFoodieActivityResource, self).get_object_list(request)
if request and hasattr(request, 'user'):
qs = qs.exclude(pk = request.user.pk)
return qs
...
关于python - Django-Tastypie : Omit one specific object to be serialized in ManyToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848110/
我正在 sitepoint 上阅读一篇文章 - http://www.sitepoint.com/how-to-create-mysql-events/关于mysql事件的主题,当你可以安排时我遇到了
我想为用户预订一个特定的时间(开始时间和结束时间),其他人不能在同一时间预订。例如:John预约了一个月5号7点到8点的时间,我想没有其他人可以在同一时间和日期预订。我想查一下时段,因为如果像John
I want to book a specific time for the user (start hour and end hour ) and no one else can book a
我已经克隆了 linux 内核 git 仓库: git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 我
我正在尝试获取下面突出显示的文本“frei ab 01.05.2017”。然而问题是,“section_content iw_right”类在该网站上出现了 19 次。我会执行 find_all 并仅
我尝试找到这个问题,但所有其他问题都与我的问题无关。 我的问题:我有类似 0xFreeFoodU 的东西,我必须获得特定的位置,然后翻转它们或将它们设为 1 或 0。 例如,位置 2、6、10、14、
我正在尝试创建一个登录功能,该功能允许我指定要从中选择数据的表以及要选择的字段。我已设法将表值传递给函数,并从所需的表中选择数据,但我的表有不同的字段。如何在查询中指定它是一个表选择: user_id
我是 Rails 的新手,在获取布局(用作唯一的静态登录页面)来加载我想与之关联的特定样式表时遇到了问题。例如:www.landing.com 加载landing.scss.erb . 作为背景,所有
我不明白为什么这两个示例的行为不同。 HTML 和 CSS 的目的只是简单地水平对齐 div,并让最后一个 div(向右)占据剩余空间(容器的剩余宽度)。 为正确的元素使用特定的 id: #left
我是 postfix 的新手。如何使用限制类或其他方法将特定用户的特定域列入黑名单。 假设我的机器有两个用户 - user1 和 user2。 我想将abc.com 到user1@mydomain 的
当记事本是 .txt 文件的默认程序时,我如何告诉 Windows 在写字板中打开 C:\test\test.txt? 最佳答案 接受的答案对我不起作用。我不确定这是因为我试图运行的程序,还是因为路径
对于下面的代码示例,我需要检查一个 tr ,type="a"并且有一个带有文本“3”作为 child 的 td ,是否存在: 1 1
var s = '-10px -10px'; var n = '33px'; 我需要一个产生这个结果的正则表达式:'-10px 33px' 类似于:s.replace(???, n) 最佳答案 如果您
一个项目 table: items 有许多分类法 table: taxonomies 使用连接表 item_taxonomies (item_id, taxonomy_id)。 使用分类组搜索项目。
h:panelGrid 表有 2 列。 这是必需的,第一列为整个表格宽度的 30%,第二列为整个表格宽度的 70%。 这种情况有一些配置吗?看起来 columnClasses 属性应用于所有列, 并且
特定阶段仅在特定计算机上卡在队列中 显示队列中的位置:1,但无法连接到代理,即使没有其他作业正在运行且队列为空 所有其他阶段都工作正常 故障阶段在不同的机器上工作正常 代理已在线并已启用 我们所有的代
我正在使用 CakePHP 3.3.10。我需要将 JavaScript 文件添加到特定 View 。 // default.ctp // I need to remove this script
首先,我发现提出问题很困难,欢迎反馈。 我必须制作一个机器学习代理来玩点和盒子。 我还处于早期阶段,但提出了一个问题:如果我让我的机器学习代理(具有特定的实现)与它自身的副本进行对抗来学习和改进它的游
我是开发 iOS 应用程序的新手,英语不是我的母语,所以请原谅我的任何错误和丑陋的代码。 我尝试创建的应用程序应该只在特定的一天显示一张特定的图像(如果日期更改,则更改图像)。因此我实现了一个检查日期
我写了一些函数,并编译成一个dll模块。在我的头文件中如下: #ifndef GET_DATAFEED_FORKDB_H #define GET_DATAFEED_FORKDB_H #include
我是一名优秀的程序员,十分优秀!