So, I think I have a neat idea for solving my problem, simply make a 
function which returns a list of path() returns.

In urls.py
urlpatterns = GetItemPaths() + [
# other paths
]

In models.py:
def GetItemPaths():    paths = [ ]
    for i in [ 'Model1', 'Model2', 'Model3' ]:
        p = path( i + '/list/', ItemListView.as_view(model=i), name=i + 
'_list' )
        paths.append(p) 

    return paths

However, there seems to be a problem with trying to pass the model name to 
my CBV ItemListView.
Unless I replace the model=i with e.g. model=Model1 I get the following 
error:

'str' object has no attribute '_default_manager'


So it certainly seems that using model=i will not pass the model name.
Is this because the argument for model= expects not a string but an object?


thanks, Mikkel

torsdag den 21. juni 2018 kl. 10.19.08 UTC+2 skrev Mikkel Kromann:
>
> I have a lot of models (say 30 or 40) which are so similar, that I have 
> handled them using slightly modified class based views.
>
> In urls.py:
> urlpatterns = [
>     path('list/<str:mName>/',            ItemListView.as_view()),
>     path('create/<str:mName>/',          ItemCreateView.as_view()),
>     path('update/<str:mName>/<pk>',      ItemUpdateView.as_view()),
>     path('delete/<str:mName>/<pk>',      ItemDeleteView.as_view()),
> ]
>
> However, I'd really like to give all my urls names so that they can be 
> easily reversed (e.g. success links).
> urlpatterns = [
>     path('list/<str:mName>/',            ItemListView.as_view(),      name
> =mName+'_list'),
> ]
>
> From what I can see, my model name mName is passed only to the view and 
> apparently not to the name constructor inside urls.py
> Also, while I do not entirely understand the reverse naming process, I 
> sense that it might not be too easy to reverse the url name if it is not 
> spelled out directly.
>
> Are there any options for handling this in urls.py?
> Or should I make a filter that will transform the model name and operation 
> name to an url and accept that I have to code url logic outside urls.py
> Or could I reprogram the path() function to make this possible?
> Or should I violate DRY and simply write 4 path lines for each of my 40 
> models?
>
>
> cheers + thanks, Mikkel
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e44a11ea-3115-4414-9af7-c1889c4f2f5c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to