Anonymous avatar Anonymous committed 63e1b16

Improve a single instance retrieving by get method

Comments (0)

Files changed (1)

django_roa/db/query.py

         except Exception, e:
             raise ROAException(e)
         
-        return int(response.body_string())
+        cnt = 0
+        try:
+            cnt = int(response.body_string())
+        except: pass
+        
+        return cnt
 
     def _get_from_id_or_pk(self, id=None, pk=None):
         """
         """
         # special case, get(id=X) directly request the resource URL and do not
         # filter on ids like Django's ORM do.
-        if kwargs.keys() == ['id']:
-            return self._get_from_id_or_pk(id=kwargs['id'])
-        elif kwargs.keys() == ['pk']: # useful for admin which relies on pks
-            return self._get_from_id_or_pk(pk=kwargs['pk'])
+        kws = kwargs.copy()
+        # check if it's a related field retrieving its model instance
+        if len(kws.keys()) == 1:
+            for kw in kws.keys():
+                if kw.endswith('__exact'):
+                    # strip the "_exact" suffix since we want to retrieve it by ID/PK
+                    kwargs[kw[:-len('__exact')]] = kws[kw]
+                    kws[kw[:-len('__exact')]] = kws[kw]
+                    del kws[kw]
+
+        if kws.keys() == ['id']:
+            del kws['id']
+            return self._get_from_id_or_pk(id=kwargs['id'], **kws)
+        elif kws.keys() == ['pk']: # useful for admin which relies on pks
+            del kws['pk']
+            return self._get_from_id_or_pk(pk=kwargs['pk'], **kws)
+        # check the case of PK attribute with custom name
+        elif kws.keys() == [self.model._meta.pk.attname]:
+            del kws[self.model._meta.pk.attname]
+            return self._get_from_id_or_pk(pk=kwargs[self.model._meta.pk.attname], **kws)
         else:
+            # filter the request rather than retrieve (get) it
             return super(RemoteQuerySet, self).get(*args, **kwargs)
 
     def latest(self, field_name=None):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.