Django Tastypie prepend_urls error
Question: Django-tastypie error. I am trying to prepend_urls so that I can list friends
for a user but I get an error **" NameError at /api/v1/friends/user/1/ global
name 'url' is not defined"**. Here is the code for the Friends Resource.

    
    
    class FriendsResource(ModelResource):
          from_user=fields.ForeignKey(UserResource,'from_user')
          to_user=fields.ForeignKey(UserResource,'to_user')
    
    
          class Meta:
               queryset=Friends.objects.all()
               serializer=Serializer(formats=['json'])
               resource_name='friends'
               filtering={
                           'from_user':ALL_WITH_RELATIONS,
                           'to_user':ALL_WITH_RELATIONS
    
               }   
    

and here is the code for prepend_urls and the method to put into wrap_view.

    
    
    def get_users(self,request):
           self.method_check(request,['get'])
           friends = []
           for friend in Friends.objects.filter(Q(from_user=request.user)|Q(to_user=request.user)):
                friends.append(friend)      
    
      def prepend_urls(self):
           return [
    
                 url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/user%s$" %(self._meta.resource_name,trailing_slash()),
                       self.wrap_view('get_users'),name= 'api_get_friends_for_user')
                   ]
    

Here is the Traceback:

    
    
      Environment:
    
    Request Method: GET
    Request URL: http://localhost:8000/api/v1/friends/user/1/
    
    Django Version: 1.6.2
    Python Version: 2.7.3
    Installed Applications:
    ('django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'tastypie',
     'userprof',
     'relations',
     'event',
     'liking',
     'feed')
    Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')
    
    
    Traceback:
    File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/handlers/base.py" in get_response
      101.                 resolver_match = resolver.resolve(request.path_info)
    File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in resolve
      318.             for pattern in self.url_patterns:
    File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in url_patterns
      346.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/core/urlresolvers.py" in urlconf_module
      341.             self._urlconf_module = import_module(self.urlconf_name)
    File "/root/python/django-zack/local/lib/python2.7/site-packages/Django-1.6.2-py2.7.egg/django/utils/importlib.py" in import_module
      40.         __import__(name)
    File "/root/python/django-zack/wyat/wyat/urls.py" in <module>
      25.      url(r'^api/',include(v1_api.urls)),
    File "/root/python/django-zack/local/lib/python2.7/site-packages/tastypie/api.py" in urls
      107.             pattern_list.append((r"^(?P<api_name>%s)/" % self.api_name, include(self._registry[name].urls)))
    File "/root/python/django-zack/local/lib/python2.7/site-packages/tastypie/resources.py" in urls
      324.         urls = self.prepend_urls()
    File "/root/python/django-zack/wyat/event/api.py" in prepend_urls
      68.                  url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/user%s$" %(self._meta.resource_name,trailing_slash()),
    
    Exception Type: NameError at /api/v1/friends/user/1/
    Exception Value: global name 'url' is not defined
    

Please tell me where I am wrong because I used the example from the cookbook
on nested resources and I can't see where I'm wrong.


Answer: @Zacmwa

I have a working prepend_urls .. you can take a look at the following example.

    
    
    
    def prepend_urls(self):
            return [
                url(r"^(?P%s)/generate%s$" %
                    (self._meta.resource_name, trailing_slash()),
                    self.wrap_view('genusr'), name="api_get_genusr"),
            ]
    
    def genusr(self, request, **kwargs):
            data = self.deserialize(request, request.body, format=request.META.get('Content-Type', 'application/json'))
            print(data.get('workflows',None))
            child_resource = UserResource()
            return child_resource.get_list(request)
    
    

And the above methods is wrappered inside my resource model.

Error is likely to be caused due to some missing imports kindly try to import
**django.core.urlresolvers import resolve, from tastypie.utils import
trailing_slash** and try again. Let me know what happens when you do this.

