Giter VIP home page Giter VIP logo

Comments (5)

chibisov avatar chibisov commented on August 12, 2024

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.

maryokhin avatar maryokhin commented on August 12, 2024

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.

chibisov avatar chibisov commented on August 12, 2024

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.

maryokhin avatar maryokhin commented on August 12, 2024

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.

auvipy avatar auvipy commented on August 12, 2024

are the discussed issued solved? or anything remain?

from drf-extensions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.