Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8471561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:49:48+00:00 2026-06-10T16:49:48+00:00

I am trying to write a Django TemplateView that returns a context parameter ‘data’

  • 0

I am trying to write a Django TemplateView that returns a context parameter ‘data’ containing JSON based on tastypie’s canonical resource:

resources

class FooResource(ModelResource):
    bars = fields.ToManyField('app.api.v1.resources.BarResource', 'bars', null=True, full=True)

    class Meta:
        queryset = Foo.objects.all()
        resource_name = 'foo'
        # ...

models

class FooDetailView(TemplateView):
    template_name = 'app/foo_detail.html'

    def get_detail(self, slug):
        foo_resource = v1_api.canonical_resource_for('foo')

        try:
            foo = foo_resource.cached_obj_get(slug=slug)
        except Foo.DoesNotExist:
            raise Http404

        bundle = foo_resource.full_dehydrate(foo_resource.build_bundle(obj=foo))
        return bundle.data

    def get_context_data(self, **kwargs):
        base = super(FooDetailView, self).get_context_data(**kwargs)
        base['data'] = self.get_detail(base['params']['slug'])
        return base

This works, however the reverse relationship between Foo and Bar doesn’t seem to get serialized by the manual process. The TemplateView returns these as strings, here’s the response:

{
    'title': u'I am Foo.title',
    'bars': [<Bundle for obj: '1' and with data: '{'title': u'I am Bar.title'}']
}

So, question, how do I iterate over the reverse relationships when building the bundle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:49:50+00:00Added an answer on June 10, 2026 at 4:49 pm

    The answer was to run bundle through the resource.serializer after dehydrating

    def get_detail(self, slug):
        qr = v1_api.canonical_resource_for('question')
        try:
            question = qr.cached_obj_get(slug=slug)
        except Question.DoesNotExist:
            raise Http404
    
        bundle = qr.full_dehydrate(qr.build_bundle(obj=question))
    
        # create response
        desired_format = qr.determine_format(self.request)
        serialized = qr.serialize(self.request, bundle, desired_format)
        return serialized
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I’m trying to write a Django query that returns objects that match either of
I'm trying to write a django inclusion tag that takes an arbitrary number of
I am trying write a function that generates simulated data but if the simulated
I'm trying to write a Django-subclassed ModelChoiceField class that knows how to load itself,
I am trying to write a test that involves running a django-tasks task. The
Hi I am trying to write a django webapp that sits on top of
I'm trying to write a custom command that works outside of Django projects. I
I've been experimenting with Django's Class Based Views and am trying to write a
I'm trying to write a Django URL pattern that matches 1 to n strings
I'm trying to write a Django query for widgets that are more than 5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.