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:
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?