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+'/<pk>/', ItemUpdateView.as_view(model=m), name=i+'_update' ) )
paths.append(path( 'delete/'+i+'/<pk>/', 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'),
]
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+unsubscribe@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.
No comments:
Post a Comment