【问题标题】:How to retrieve an object that was just created in Tastypie如何检索刚刚在 Tastypie 中创建的对象
【发布时间】:2012-12-18 18:17:12
【问题描述】:

使用 Tastypie 和 Django 用户流,我正在尝试构建活动提要。我已经成功构建了以下收藏功能:

class LikeResource(ModelResource):
user = fields.ForeignKey(BasicUserResource, 'user', full=True)  
class Meta:
    queryset = Journalist.objects.all()
    allowed_methods = ['get', 'put']
    resource_name = 'like'
    fields = ['user']
    default_format = "application/json"
    authorization = Authorization()
    authentication = BasicAuthentication()
    serializer = Serializer(formats=['json'])
    always_return_data = True
    include_resource_uri = False

def hydrate(self, bundle):
    shot = LifeShot.objects.all().get(id = bundle.data['post id'])
            if(bundle.obj.likes.filter(id = bundle.data['post id']).exists()):
                 bundle.obj.likes.remove(shot)
            else:
                 bundle.obj.likes.add(shot)
                 user_doing_the_liking=User.objects.get(username=bundle.request.user.username)
                 user_getting_liked = shot.journalist.user
                 user_streams.add_stream_item(user_getting_liked, '%s liked your shot %s %s' % (bundle.request.user.username, shot.url, datetime.datetime.utcnow()))
            return bundle

    def dehydrate(self, bundle):
         shot = LifeShot.objects.all().get(id = bundle.data ['post id'])
         user_getting_liked = shot.foodie.user
         likeitems = user_streams.get_stream_items(user_getting_liked)
         list = ''
         for likeitem in likeitems:
              list = list + likeitem.content +', '
         bundle.data['likestream'] = list
         return bundle

现在要评论一张照片,这是我目前所拥有的:

class CommentEntryResource(ModelResource):
user = fields.ForeignKey(BasicJournalistResource, 'user', full =True)
picture = fields.ForeignKey(BasicLifeShotResource, 'picture', full=True)
class Meta:
    queryset = Comment.objects.all()
    allowed_methods = ['post', 'get','put']
    resource_name = 'comment'
    fields = ['comment', 'picture', 'user']
    authorization = Authorization()
    authentication = BasicAuthentication()
    serializer = Serializer(formats=['json'])
    include_resource_uri = False
    always_return_data = True

    filtering = {
            'picture': ALL_WITH_RELATIONS,
    }

def hydrate_user(self, bundle):

    bundle.data['user'] = Journalist.objects.get(user = bundle.request.user)


    return bundle   

现在唯一的区别是 Tastypie 创建了一个新的评论对象。如何检索该评论对象并使用 Django 用户流实现它,以便在我的提要中显示“用户 1 评论了您的镜头:看起来不错!” ???

Django 用户流:https://github.com/dabapps/django-user-streams 好吃的:http://django-tastypie.readthedocs.org/en/latest/

【问题讨论】:

    标签: django backend tastypie


    【解决方案1】:

    试试这个:

     bundle.data['user'] = Journalist.objects.get(user = bundle.obj.user)
    

    bundle.obj 会给你Comment 模型对象。您可以通过bundle.obj.user获取用户。

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 2020-03-24
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      相关资源
      最近更新 更多