Dear Django users.
I'm testing whether Django is a good choice for a web based application that I need to handle (create, read, update, delete) a lot of data tables.
I've discovered the built-in Class Based View, which are going to save me from a lot of repetitive code lines - yay!
Now, I've got an awful lot of models for my data tables (well, around 20-30 ...) and looking at my code so far, it strikes me that it will quickly become a bit repetitive, which I agree is a bad thing.
From urls.py:
urlpatterns = [
# Urls for truck forms & actions
path('data/trucks/list/',views.TruckListView.as_view(), name='truck_list'),
path('data/trucks/create/',views.TruckCreateView.as_view(), name='truck_create'),
path('data/trucks/update/<pk>/',views.TruckUpdateView.as_view(), name='truck_update'),
path('data/trucks/delete/<pk>/',views.TruckDeleteView.as_view(), name='truck_delete'),
# Urls for container forms & actions
path('data/containers/list/',views.ContainersListView.as_view(), name='containers_list'),
path('data/containers/create/',views.ContainersCreateView.as_view(), name='containers_create'),
path('data/containers/update/<pk>/',views.ContainersUpdateView.as_view(), name='containers_update'),
path('data/containers/delete/<pk>/',views.ContainersDeleteView.as_view(), name='containers_delete'),
# Urls for destination forms & actions
# ... guess what will come here ...
]
From views.py
# Create views
class TruckCreateView(CreateView):
model = Truck
fields = ['label', 'description', 'max_speed', 'max_weight', 'max_volume' ]
def get_success_url(self):
return reverse("truck_list")
class ContainerCreateView(CreateView):
model = Container
fields = ['label', 'description', 'max_weight', 'max_volume' ]
def get_success_url(self):
return reverse("container_list")
# ... drum-whirl ... what is next? ...
I suppose my task at hand may be a bit unusual, but I aIso think I can't be the first facing this problem.
Basically, I want to do more or less the same with all my models (maybe in 2 or 3 variations). So,
1) did someone perhaps already make a nice package of Class Based Views which can take an Model name as argument, with the model capable of providing the necessary data, e.g. field list?
2) How advisable / possible would it be to take the object name from the url and pass that on to the generic class based view?
3) Did I miss something very obvious? :)
Thanks for your help, Mikkel