NoneType object has no attribute in POST/PUT a resource with nested resources (django, tastypie)

193 views
Skip to first unread message

Alberto Díaz

unread,
May 10, 2012, 6:32:35 AM5/10/12
to django-d...@googlegroups.com
Hi all,

I've been digging in this problem and haven't found how to solve it. I've got two secure resources, ComplementoResource and ProductoResource defined as follow:

class ComplementoResource(CommonModelResource):
  restaurante = fields.ForeignKey(RestauranteResource, 'restaurante')
  restaurante_id = fields.IntegerField(attribute='restaurante_id')

  class Meta:
    queryset = Complemento.objects.all()
    resource_name = 'complementos'
    list_allowed_methods = ['get', 'post', 'put', 'delete']
    authentication = tastypie.authentication.ApiKeyAuthentication()
    authorization = tastypie.authorization.Authorization()

  def alter_list_data_to_serialize(self, request, data):
    return self.alter_json_list(data, 'complementos')

  def apply_authorization_limits(self, request, object_list):
    return object_list.filter(restaurante__user=request.user)


class ProductoResource(CommonModelResource):
  restaurante = fields.ForeignKey(RestauranteResource, 'restaurante')
  restaurante_id = fields.IntegerField(attribute='restaurante_id')
  complementos = fields.ManyToManyField(ComplementoResource, 'complementos', full=True)

  class Meta:
    queryset = Producto.objects.all()
    resource_name = 'productos'
    list_allowed_methods = ['get', 'post', 'put', 'delete']
    authentication = tastypie.authentication.ApiKeyAuthentication()
    authorization = tastypie.authorization.Authorization()

  def alter_list_data_to_serialize(self, request, data):
    return self.alter_json_list(data, 'productos')

  def apply_authorization_limits(self, request, object_list):
    return object_list.filter(restaurante__user=request.user)

The problem comes when I try to post a new entity Producto with one or more entities from Complemento. I get "'NoneType' object has no attribute 'user'" error message. The request parameter in the method apply_authorization_limits of CombinadoResource get's a value of None. After digging a bit, I've seen that in the  file fields.py, there is a method called resource_from_data used, and in line 548 (at least in my version) that says:

return fk_resource.obj_update(fk_bundle, **data)

This method has the correct request value, but doesn't pass it to fk_resource.obj_updat, so it get's the default value (None) and after two or three mor calls the error raises. And the funny thing is that I get an error but the new entity Producto is inserted (without entities of Complementos).

Anybody knows if I'm going in the wrong way and this things have to be done otherwise? And the other question, anybody knows how to abort the insertion of the entity Producto if entities of Complement are not inserted?

Thank you all very much

Alberto

Karen Tracey

unread,
May 10, 2012, 8:04:10 AM5/10/12
to django-d...@googlegroups.com
Please ask questions about using Django on django-users, not here. The topic of this list is the development of Django itself. (Also when you post to django-users for this problem it would help people help you if you included the full traceback you are getting.)

Karen

Alberto Díaz

unread,
May 14, 2012, 7:44:51 AM5/14/12
to django-d...@googlegroups.com
Thanks and sorry Karen
Reply all
Reply to author
Forward
0 new messages