Re: Is it possible to construct reversible url names in urls.py using , regex etc

2018-06-25 Thread Mikkel Kromann
Thank you so much for your help, Melvyn.
With your help I managed to the the code running :)
Here it is, if somebody should find it helpful.

cheers, Mikkel

>From somewhere, e.g. models.py
# The list of model names (just add your model names to it)
def GetItemNames():
return [ 'Year', 'Vintage', 'Region', 'Location' ] 

# The generator of paths
def GetItemPaths():
paths = [ ]
for i in GetItemNames():
m = apps.get_model('items', i)
paths.append(path( 'list/'+i+'/' , ItemListView.as_view(
model=m),  name=i+'_list'   ) )
paths.append(path( 'create/'+i+'/',ItemCreateView.as_view(
model=m),name=i+'_create' ) )
paths.append(path( 'update/'+i+'//',   ItemUpdateView.as_view(
model=m),name=i+'_update' ) )
paths.append(path( 'delete/'+i+'//',   ItemDeleteView.as_view(
model=m),name=i+'_delete' ) )

return paths

>From urls.py:
from . models import GetItemPaths
urlpatterns = GetItemPaths() + [
path('/to/some/other/views/'otherview.asView(), name='other_name'),
]




lørdag den 23. juni 2018 kl. 15.08.04 UTC+2 skrev Melvyn Sopacua:
>
> On zaterdag 23 juni 2018 14:30:08 CEST Mikkel Kromann wrote: 
>
> > 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? 
>
> Yes. You can use import_module from django.functional to pass a dotted 
> path. 
> Or you can use apps.get_model() to get a model class for a app name / 
> model 
> name combo. 
>
> -- 
> Melvyn Sopacua 
>

-- 
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/72b5c5e6-b07d-41b8-a9b5-6a471ffe65cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Is it possible to construct reversible url names in urls.py using , regex etc

2018-06-23 Thread Melvyn Sopacua
On zaterdag 23 juni 2018 14:30:08 CEST Mikkel Kromann wrote:

> 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?

Yes. You can use import_module from django.functional to pass a dotted path. 
Or you can use apps.get_model() to get a model class for a app name / model 
name combo.

-- 
Melvyn Sopacua

-- 
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/2271710.2KSSaTsjz5%40fritzbook.
For more options, visit https://groups.google.com/d/optout.


Re: Is it possible to construct reversible url names in urls.py using , regex etc

2018-06-23 Thread Mikkel Kromann
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//',ItemListView.as_view()),
> path('create//',  ItemCreateView.as_view()),
> path('update//',  ItemUpdateView.as_view()),
> path('delete//',  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//',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.


Re: Is it possible to construct reversible url names in urls.py using , regex etc

2018-06-21 Thread Melvyn Sopacua
On donderdag 21 juni 2018 10:19:08 CEST Mikkel Kromann wrote:

> 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//',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?

Take a look at crudlfap's Router class to see how to generate dynamic urls. In 
short: let a router who generates views for models generate the urlpatterns. 
All django is looking for is an iterable that is named 'urlpatterns' in a urls 
module.

https://github.com/yourlabs/crudlfap/blob/master/src/crudlfap/router.py#L274
-- 
Melvyn Sopacua

-- 
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/7023284.eSgYguR0o7%40fritzbook.
For more options, visit https://groups.google.com/d/optout.


Is it possible to construct reversible url names in urls.py using , regex etc

2018-06-21 Thread 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//',ItemListView.as_view()),
path('create//',  ItemCreateView.as_view()),
path('update//',  ItemUpdateView.as_view()),
path('delete//',  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//',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/9cc50a2b-b613-4427-a8e4-101f9bf394d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.