如何从 Django Tastypie 中的相关模型返回数据?

发布于 2024-12-04 06:57:29 字数 2002 浏览 0 评论 0原文

如何从另一个模型引入信息?

我有两个模型 ArticleArticleBody

Article 包含主要信息,ArticleBody 包含正文和图像信息循环

class Article(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    excerpt = models.CharField(max_length=140, null=True, blank=True, help_text='A description no longer than 140 characters that explains what the article is about, important for SEO')
    category = models.ManyToManyField(Category)
    date_published = models.DateTimeField()
    slug = models.SlugField(null=True)
    status = models.CharField(choices=STATUS, max_length=2, default='DR')
    tags = TagField(default='', null=True, blank=True, help_text='Just add a comma between the tags i.e. "My very important name, Hunting, Scope, Rifle"')
    source_name = models.CharField(default='', blank=True, null=True, help_text='Outdoor Magazine', max_length=100)
    source_url = models.URLField(verify_exists=False, max_length=200, null=True, blank=True, help_text='http://www.source.com/2011/01/long-name/')

class ArticleBody(ImageModel):
    article = models.ForeignKey(Article)
    body = models.TextField(verbose_name='', blank=True, null=True)
    image = models.ImageField(storage=cloudfiles_storage, upload_to='articles', default='avatar-blank.jpg', verbose_name='', blank=True, null=True)
    caption = models.CharField(max_length=80, null=True, blank=True)

在我的 api resources.py 文件中,我试图获取ArticleBody 信息放入我的 NewsResource...

这是我到目前为止所拥有的。

class NewsBodyResource(ModelResource):
    class Meta:
        queryset = ArticleBody.objects.all()
        resource_name = 'article_body'

class NewsResource(ModelResource):

    class Meta:
        queryset = Article.objects.filter(status='PU', date_published__lt=datetime.datetime.now).order_by('-date_published')
        resource_name = 'news'

进行更改以便我可以将 ArticleBody 循环放入我的 NewsResource 中的正确 TastyPIE 方法是什么?

How do I bring in the information from another model?

I have two models Article, and ArticleBody

Article containing the main info and ArticleBody containing a loop of body and image information

class Article(models.Model):
    author = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    excerpt = models.CharField(max_length=140, null=True, blank=True, help_text='A description no longer than 140 characters that explains what the article is about, important for SEO')
    category = models.ManyToManyField(Category)
    date_published = models.DateTimeField()
    slug = models.SlugField(null=True)
    status = models.CharField(choices=STATUS, max_length=2, default='DR')
    tags = TagField(default='', null=True, blank=True, help_text='Just add a comma between the tags i.e. "My very important name, Hunting, Scope, Rifle"')
    source_name = models.CharField(default='', blank=True, null=True, help_text='Outdoor Magazine', max_length=100)
    source_url = models.URLField(verify_exists=False, max_length=200, null=True, blank=True, help_text='http://www.source.com/2011/01/long-name/')

class ArticleBody(ImageModel):
    article = models.ForeignKey(Article)
    body = models.TextField(verbose_name='', blank=True, null=True)
    image = models.ImageField(storage=cloudfiles_storage, upload_to='articles', default='avatar-blank.jpg', verbose_name='', blank=True, null=True)
    caption = models.CharField(max_length=80, null=True, blank=True)

In my api resources.py file I am trying to get the ArticleBody information into my NewsResource...

This is what I have so far.

class NewsBodyResource(ModelResource):
    class Meta:
        queryset = ArticleBody.objects.all()
        resource_name = 'article_body'

class NewsResource(ModelResource):

    class Meta:
        queryset = Article.objects.filter(status='PU', date_published__lt=datetime.datetime.now).order_by('-date_published')
        resource_name = 'news'

What is the correct TastyPIE way, of making changes so I can get a loop of ArticleBody into my NewsResource?

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

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

发布评论

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

评论(1

九命猫 2024-12-11 06:57:30
class NewsBodyResource(ModelResource):
    class Meta:
        queryset = ArticleBody.objects.all()
        resource_name = 'article_body'

class NewsResource(ModelResource):
    newsbodies = fields.ToManyField('yourapp.api.resources.NewsBodyResource', 'articlebody_set', full=True)

    class Meta:
        queryset = Article.objects.filter(status='PU', date_published__lt=datetime.datetime.now).order_by('-date_published')
        resource_name = 'news'

ToManyField 的参数分别表示以下内容:

  1. 表示集合的资源的项目相对导入路径

  2. 名称字段(如果位于父模型或 related_name 上)
    字段的属性(如果位于子模型上)

  3. 是否嵌入
    将每个子项的完整数据放入 feed (True) 或仅放入资源
    链接到每个子项(False)

class NewsBodyResource(ModelResource):
    class Meta:
        queryset = ArticleBody.objects.all()
        resource_name = 'article_body'

class NewsResource(ModelResource):
    newsbodies = fields.ToManyField('yourapp.api.resources.NewsBodyResource', 'articlebody_set', full=True)

    class Meta:
        queryset = Article.objects.filter(status='PU', date_published__lt=datetime.datetime.now).order_by('-date_published')
        resource_name = 'news'

The parameters to ToManyField represent the following respectively:

  1. project-relative import path to the resource representing the set

  2. the name of field if it's on the parent model or the related_name
    attribute of the field if it's on the child model

  3. whether or not to embed
    the full data of each child into the feed (True) or just resource
    links to each child (False)

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