I've created a BaseResource
class BaseResource(ModelResource):
def wrap_view(self, view):
@csrf_exempt
def wrapper(request, *args, **kwargs):
try:
callback = getattr(self, view)
return callback(request, *args, **kwargs)
except IntegrityError, e:
return HttpResponse(e, status=300,
reason='Internal Server error')
return wrapper
class Meta:
allowed_methods = ['get', 'post', 'put']
list_allowed_methods = ['get', 'post', 'put']
detail_allowed_methods = ['get', 'post', 'put']
include_resource_uri = False
default_format = 'application/json'
always_return_data = True
throttle = BaseThrottle(throttle_at=3, timeframe=10,
expiration=1)
authentication = MultiAuthentication(SessionAuthentication(),
ApiKeyAuthentication())
authorization = Authorization()
serializer = urlencodeSerializer()
There is my resources:
class UserResource(BaseResource):
groups = fields.ToManyFields('GroupResource', 'group')
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['password']
authorization = Authorization()
allowed_methods = ['get', 'post', 'put']
filtering = {'username': ALL, 'email': ALL}
class GroupResource(BaseResource):
user = fields.ForeignKey(UserResource, 'user')
permissions = fields.ToManyFields('PermissionResource','permissions_set', related_name='permission')
class Meta:
queryset = Group.objects.all()
allowed_methods = ['get', 'post', 'put']
resource_name = 'group'
class PermissionResource(BaseResource):
group = fields.ToOneField('GroupResource', 'group_set')
class Meta:
queryset = Permission.objects.all()
allowed_methods = ['get']
Im trying to create a Resource for the user that extends BaseResource, but when i make the relations i get the following error:
groups = fields.ToManyFields('GroupResource', 'group')
AttributeError: 'module' object has no attribute 'ToManyFields'
I've searched but I cant find nothing that can relp me. Any clues? What im doing wrong? Everything is welcome. Thanks!
The AttributeError
is telling you exactly what's wrong:
For your group resource:
class GroupResource(BaseResource):
user = fields.ForeignKey(UserResource, 'user')
permissions = fields.ToManyFields('PermissionResource','permissions_set', related_name='permission')
You are using ToManyFields
, which you haven't defined in GroupResource
, meaning that it would have to come from a super class, starting with BaseResource (where it is not defined), and ModelResource, which is not pasted here.
While you could debug and look for where this method is actually defined, it seems as if the field that you are trying to use is not in the Django documentation, but is in the TastyPie documentation.
You seem to have a typo. The Django-TastyPie field is actually named ToManyField
.