我有一个简单的聊天应用程序。这是我的模型。
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。这是我的应用程序当前如何工作的示例,如果史蒂夫想向比尔发送消息,则会创建一个新的“消息”对象,其中史蒂夫是发件人。创建消息对象时,它还会创建一个“线程”对象。在这个线程中,参与者是史蒂夫和比尔。
我目前有一个 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"
}],
}]
}
当数据返回时,我不想包括用户史蒂夫,因为他是 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 的数据?