Hey, Thank you for your feedback.
Making a new URL definition is a good approach, even though the example I gave would require a few extra lines, that may be more readable than squeezing everything together. Regarding the custom converter, I already have custom converters in place, and I don't think making a new one for every combination would be better, my initial goal was to reuse those converters as they can be properly tested. Anyway, multiple url definitions it is, thank you all again. On Saturday, August 31, 2019 at 10:32:35 AM UTC+1, Adam Johnson wrote: > > Hi Marco, > > Thanks for writing a clear, short proposal. > > This could be neat, but I'm against it for these reasons: > > - It's extra syntax - the advantage of the new URL syntax is that it's > very simple and easy to pick up. It's copied from Flask, which also > doesn't > support a "multiple converter" syntax ( > https://flask.palletsprojects.com/en/1.1.x/quickstart/#routing ). > Instead, you're expected to define multiple routes if that's what you want. > - It is only a shortcut - the behaviour you want can be achieve with > multiple URL definitions. This is clearer in terms of readability and > precedence (top to bottom). Understanding precedence could be a problem in > particular for a multi-converter pattern like "<int|str:a>/<int|str:b>/" - > does "int a, str b" have precedence over "str a, int b", or the other way > round? > - You can define a custom converter right now, as Richard suggests. > > Thanks for taking the time to write to the mailing list though, > > Adam > > On Fri, 30 Aug 2019 at 18:30, Richard <rea...@asymmetricventures.com > <javascript:>> wrote: > >> A possibility without changes to Django is to create a generic converter >> and apply it to each model: >> >> class ModelUUIDConverter: >> regex= r'[a-fA-F0-9]{32}' >> def __init__(self, model): >> self.model = model >> def to_python(self, value): >> return self.model.objects.filter(Q(uuid=value)|Q(display_id=value). >> get() >> def to_url(self,value): >> return str(value) >> >> urls.register_converter(lambda: ModelUUIDConverter(TicketModel), >> 'ticket_or_uuid') >> >> path('api/<ticket_or_uuid:ticket>',TicketDetailView.as_view()) >> >> >> >> On Friday, 30 August 2019 11:12:08 UTC-6, Marco Silva wrote: >>> >>> Hello, >>> >>> I'm on the process of migrating an app to django >2 and walked into this >>> potencial new feature while converting the URLs. >>> >>> Some modeles use a "display" id, like TICKET-2019-002, but it's pk is >>> actually a uuid, so the url would ideally support both. >>> >>> I've created a custom converter to verify the correct value, therefore I >>> can use: >>> `path('api/ticket/<ticket:ticket>', TicketDetailView.as_view())` >>> >>> but if i want to also expose the api with the uuid, I have to add >>> another urlpattern with: >>> `path('api/ticket/<uuid:ticket>', TicketDetailView.as_view())` >>> >>> my proposal would be to add support for this: >>> `path('api/ticket/<ticket|uuid:ticket>', TicketDetailView.as_view())` >>> >>> >>> The concrete example is a different use case, its a "common" view where >>> you must specify the type first, so basically: >>> `path('api/<str:obj_type>/<ticket|task|epic|uuid:refecence_nr>/assign', >>> GenericAssignView.as_view())` >>> >>> >>> From what i've seen on the API, wouldn't be much work to add regex >>> groups on the djanjo.urls.resolvers._route_to_regex method, but it also >>> returns a converters dictionary with a mapping of parameters(the ticket, or >>> refecence_nr) with a single converter, and that could cause problems >>> somewhere... >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django developers (Contributions to Django itself)" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-d...@googlegroups.com <javascript:>. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-developers/d7cb9137-98bf-4db0-993b-538000321c65%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-developers/d7cb9137-98bf-4db0-993b-538000321c65%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Adam > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/8bb84ddf-cd1d-4461-92d7-fb7fc601a4d4%40googlegroups.com.