Comments (5)
Hi @frwickst.
I think we should somehow reuse default get_lookup_regex. Then you could specify regex of your pk lookup right in the viewset:
class MyModelViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
lookup_value_regex = '.+@.+\..+' # super email regex
Don't hesitate to make a pull request =)
from drf-extensions.
Hi @chibisov.
I'm very interesting in investing time in this issue, because I'm fighting with a use-case to register two sets of urls using something like:
user_pk_routes = router.register('users', UserViewSet, base_name='user')
user_username_routes = router.register('users', UserViewSet, base_name='user.username')
and then attaching nested routes to the user with lookups by username or pk:
user_pk_routes.register('posts', PostViewSet, base_name='user-post', parents_query_lookups=['user'])
...
user_username_routes.register('posts', PostViewSet, base_name='user.username-post', parents_query_lookups=['user__username'])
In effect I want:
r'^users/(?P<pk>[0-9]+)/posts/$'
r'^users/(?P<username>[^/.]*[a-zA-Z][^/.]*)/posts/$'
By using a simple for loop I would, in theory, be able create two sets of routes for the same viewset.
The issue I have right now is that I have 2 non-conflicting regexes for username and pk (I don't allow to register usernames that would look like pk), but since the parent regex that is used is always [^/.]
either the username or the pk lookup fails, depending on which ones goes first in the urlconf.
Then you could specify regex of your pk lookup right in the viewset
The good idea about this approach is that we are reusing functionality from DRF, the bad idea is that I am forced to create a i.e. UserUsernameViewSet
and extend from UserViewSet
because they have the same logic, but different routing rules, which I would like to avoid, as it's not DRY at all.
from drf-extensions.
Hi @maryokhin
Why do you think you break DRY by extending UserViewSet
? For example, you could iteratively create new viewsets based on UserViewSet
and register them in router (example below doesn't evaluates base_name
correctly):
for _lookup_field, _lookup_value_regex in [
('pk', '[0-9]+'),
('username', '[^/.]*[a-zA-Z][^/.]*')
]:
class ExtendedUserViewSet(UserViewSet):
lookup_field = _lookup_field
lookup_value_regex = _lookup_value_regex
(
router.register('users', ExtendedUserViewSet, base_name='user')
.register('posts', PostViewSet, base_name='user.post', parents_query_lookups=['user__username'])
)
Anyway I think first of all we should implement usage of get_lookup_regex
.
from drf-extensions.
I haven't thought of dynamic view creation, maybe it is a solution. It would become a bit messy with dynamically handling base_name
and parents_query_lookups
for nested routes, which is why #55 might be in a way related.
I have also tried to propose allowing to specify viewset instances on the router, which would have allowed to make it less awkward, along the lines of:
for _lookup_field, _lookup_value_regex in [
('pk', '[0-9]+'),
('username', '[^/.]*[a-zA-Z][^/.]*')
]:
(
router.register('users', UserViewSet.as_view(lookup_field = _lookup_field, lookup_value_regex = _lookup_value_regex), base_name='user')
.register('posts', PostViewSet, base_name='user.post', parents_query_lookups=['user__username'])
)
But I'm guessing that's not going to happen.
Anyway I think first of all we should implement usage of get_lookup_regex.
Agree with this as being a good starting point, I will start looking into it.
from drf-extensions.
are the discussed issued solved? or anything remain?
from drf-extensions.
Related Issues (20)
- DRF-extensions queryset use select_related,show different sql result
- UniqueFieldsMixin should not validate field when `required=False` HOT 1
- Bulk create
- Django Cron Abandoned HOT 4
- Not compatible with django 3.2 HOT 1
- NestedViewSetMixin not working in custom actions
- My Cron Job runs twice
- Release compatible with Django 3.2 HOT 3
- "'Response' object has no attribute '_headers'" HOT 4
- Release the package with Django 3.2 support HOT 1
- get children is too slow! cache seem not very high perform
- distutils has been deprecated in Python 3.10 in favour of setuptools HOT 1
- We are getting AttributeError: 'Response' object has no attribute '_headers' HOT 2
- Nested router lookup by multiple fields HOT 1
- 500 error returns in case of invalid parent lookup HOT 3
- cache_response get error: inner() missing 1 required positional argument: 'request'
- Is this project still supported? HOT 5
- Can't get the nested routing to work after reading the docs.
- Will this library still be maintained and have new versions released? HOT 7
- Tests suite is failing with DRF 3.15.0 and beyond
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drf-extensions.