django-piston:如何获取多对多字段的值?

发布于 2024-09-05 10:17:49 字数 136 浏览 3 评论 0原文

我有一个模型,其中 ManyToManyField 到另一个模型。我想获取通过 JSON 返回的特定记录的所有信息(包括其他模型的相关信息)。

如何让 django-piston 显示这些值?我会很高兴只使用主键。 或者你能建议另一种选择吗?

I have a model with ManyToManyField to another model. I would like to get all the info on a particular record (including the related info from other models) return by JSON.

How to get django-piston to display those values? I would be happy with just primary keys.
Or can you suggest another option ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沧桑㈠ 2024-09-12 10:17:50

我可能是错的,但应该这样做:

class PersonHandler(BaseHandler):
    model = Person
    fields = ('id', ('friends', ('id', 'name')), 'name')

    def read(self, request):
        return Person.objects.filter(...)

I may be wrong, but this should do it:

class PersonHandler(BaseHandler):
    model = Person
    fields = ('id', ('friends', ('id', 'name')), 'name')

    def read(self, request):
        return Person.objects.filter(...)
追星践月 2024-09-12 10:17:50

您需要在返回多对多数据的处理程序上定义一个类方法,我不相信 Piston 会自动执行此操作。

class MyHandler(BaseHandler):
    model = MyModel
    fields = ('myfield', 'mymanytomanyfield')

    @classmethod
    def mymanytomanyfield(cls, myinstance):
        return myinstance.mymanytomanyfield.all()

You need to define a classmethod on the handler that returns the many-to-many data, I don't believe Piston does this automatically.

class MyHandler(BaseHandler):
    model = MyModel
    fields = ('myfield', 'mymanytomanyfield')

    @classmethod
    def mymanytomanyfield(cls, myinstance):
        return myinstance.mymanytomanyfield.all()
蓝颜夕 2024-09-12 10:17:50

我的代码:

型号:

class Tag(models.Model):
"""docstring for Tags"""
tag_name = models.CharField(max_length=20, blank=True)
create_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
    return self.tag_name
class Author(models.Model):
"""docstring for Author"""
name = models.CharField(max_length=30)
email = models.EmailField(blank=True)
website = models.URLField(blank=True)

def __unicode__(self):
    return u'%s' % (self.name)

class Blog(models.Model):
"""docstring for Blogs"""
caption = models.CharField(max_length=50)
author = models.ForeignKey(Author)
tags = models.ManyToManyField(Tag, blank=True)
content = models.TextField()
publish_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return u'%s %s %s' % (self.caption, self.author, self.publish_time)

手柄:

class BlogAndTagsHandler(BaseHandler):
allowed_methods = ('GET',)
model = Blog
fields = ('id' 'caption', 'author',('tags',('id', 'tag_name')), 'content', 'publish_time', 'update_time')

def read(self, request, _id=None):
    """
    Returns a single post if `blogpost_id` is given,
    otherwise a subset.

    """
    base = Blog.objects

    if _id:
        return base.get(id=_id)
    else:
        return base.all() # Or base.filter(...)

效果很好。

My code:

Models:

class Tag(models.Model):
"""docstring for Tags"""
tag_name = models.CharField(max_length=20, blank=True)
create_time = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
    return self.tag_name
class Author(models.Model):
"""docstring for Author"""
name = models.CharField(max_length=30)
email = models.EmailField(blank=True)
website = models.URLField(blank=True)

def __unicode__(self):
    return u'%s' % (self.name)

class Blog(models.Model):
"""docstring for Blogs"""
caption = models.CharField(max_length=50)
author = models.ForeignKey(Author)
tags = models.ManyToManyField(Tag, blank=True)
content = models.TextField()
publish_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)

def __unicode__(self):
    return u'%s %s %s' % (self.caption, self.author, self.publish_time)

Handle:

class BlogAndTagsHandler(BaseHandler):
allowed_methods = ('GET',)
model = Blog
fields = ('id' 'caption', 'author',('tags',('id', 'tag_name')), 'content', 'publish_time', 'update_time')

def read(self, request, _id=None):
    """
    Returns a single post if `blogpost_id` is given,
    otherwise a subset.

    """
    base = Blog.objects

    if _id:
        return base.get(id=_id)
    else:
        return base.all() # Or base.filter(...)

Works petty good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文