GithubHelp home page GithubHelp logo

Comments (6)

escalant3 avatar escalant3 commented on May 17, 2024

It looks like a bug. Could you please provide more information? It would be really useful to have:

  • The full payload that is coming back from your server.
  • The model configuration (if possible both DS.Model and Django Resources).
  • Any modifications over the default serializer and adapter.

You can send me an email if you find that easier.

Thanks!

from ember-data-tastypie-adapter.

hnqlv avatar hnqlv commented on May 17, 2024

@riklaunim @escalant3 I'm having exactly the same issue. I did a bit of investigation here and the issue happens when resourceUri is not a string, it's an object.

In my case, I found where the problem is. This is how the ember model looks like:

var Home = DS.Model.extend({
  appName: DS.attr('string'),
  appNamespace: DS.attr('string'),
  product: DS.attr('string'),
  published: DS.attr('boolean'),
  hero: DS.belongsTo('HomeHero'),
  sections: DS.hasMany('Section', {async: true})
});

The application breaks when trying to normalize the relationships:

Error while loading route: TypeError: undefined is not a function
    at DS.DjangoTastypieSerializer.DS.RESTSerializer.extend.resourceUriToId (vendor/ember-data-tastypie-adapter/packages/ember-data-tastypie-adapter/lib/tastypie_serializer.js:46:24)
    at eval (vendor/ember-data-tastypie-adapter/packages/ember-data-tastypie-adapter/lib/tastypie_serializer.js:73:27)
    at Array.forEach (native)
    at eval (vendor/ember-data-tastypie-adapter/packages/ember-data-tastypie-adapter/lib/tastypie_serializer.js:72:21)

I haven't created the HomeHero model in the backend, instead I used the dehydrate() method to add it as custom value.

Bellow is how the resource looks like. When I comment the dehydrate function everything works as expected.

class HomepageResource(ModelResource):
  class Meta:
    #authentication = MultiAuthentication(SessionAuthentication(), ACSAuthentication())
    queryset = WebApp.objects.filter(published=True)
    resource_name = 'home'
    allowed_methods = ['get']
    always_return_data = True
    detail_uri_name = 'app_namespace'
    fields = [
      'id',
      'app_name',
      'app_namespace',
      'google_analytics_code',
      'image_height',
      'image_width',
      'ooayala_player_id',
      'product',
      'published',
      'site',
      'thumbnail_height',
      'thumbnail_width',
      'hero_title',
      'hero_oneline',
      'hero_headline',
      'hero_image',
    ]

  def prepend_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/(?P<id>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
    ]

  def dehydrate(self, bundle):
    bundle.data['hero'] = {
      "id": bundle.data['id'],
      "title": bundle.data['app_name'],
      "oneline": bundle.data['hero_oneline'],
      "headline": bundle.data['hero_headline'],
      "image": bundle.data['hero_image']
    }

    bundle.data.pop('hero_oneline', None)
    bundle.data.pop('hero_headline', None)
    bundle.data.pop('hero_image', None)
    bundle.data['sections'] = [x.id for x in Section.objects.filter(webapp=bundle.obj)]
    return bundle

I believe if I create this related model things will work properly but then I lost one of the Tastypie's goodies. Before do anything I'd like to hear from you.

Cheers

from ember-data-tastypie-adapter.

riklaunim avatar riklaunim commented on May 17, 2024

I don't have all the code with me now but the case was rather simple. Poll model and Option model with ForeignKey to Poll.

The Poll resource has

options = fields.ToManyField('poll.resources.OptionResource', 'option_set', full=True)

So it has a list:

"options": [{"bar_width": 60, "id": 10, "resource_uri": "/api/v1/option/10/", "text": "Zabytkowa kamienica w Lublinie", "votes_count": 3}...]

ember Poll model had:

options: DS.hasMany('option'),

to make it work I hacked resourceUriToId to:

resourceUriToId: function (resourceUri) {
    if (resourceUri.hasOwnProperty('resource_uri')) {
        resourceUri = resourceUri.resource_uri;
    }
    return resourceUri.split('/').reverse()[1];
  },

and had to change relation to async:

options: DS.hasMany('option', {async: true}),

but that's just a workaround for the problem.

from ember-data-tastypie-adapter.

snanda85 avatar snanda85 commented on May 17, 2024

I had the same problem.
To make it work, I had to remove full=True from tastypie resource and add async: true in DS.hasMany

But the limitation now is that I have to make a separate resource in tastypie to be included with a relationship.

Now, I need to append a resource in dehydrate() just like @henriquea and there is no use-case to expose the resource directly through a separate url.

I have just started using this adaptor and not sure that it supported "embedded" resources (with full=True) before this bug?

from ember-data-tastypie-adapter.

escalant3 avatar escalant3 commented on May 17, 2024

Embedded resources have been supported but their implementation has changed a lot during ember-data development.

I do not see a unique use case that could be implemented here to provide a solution for everybody. The original idea of the adapter was being able to use it with the defaults without modifying the Tastypie resources.

If you guys agree in a solution I'd be happy to include it in the adapter. To be honest I have not used Django+Ember in a while so I would really appreciate your feedback in what it's needed.

From the comments I understand that currently full=True without async: true is not supported and needs to be added. Is that right?

from ember-data-tastypie-adapter.

riklaunim avatar riklaunim commented on May 17, 2024

Yes, it seems you can't do full=True without async, so it spawns multiple requests which in many cases is a bad thing.

If you don't need to map the objects from relation as ember model instances then you could just return a list (or a dictionary) in Tastypie and used DS.attr() instead of DS.hasMany in ember - that would just decode the json giving raw values. That works for nearly only read-only usage (you can't call update or delete on the element, but you can use it's ID to request some operations).

from ember-data-tastypie-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.