Sunday, April 29, 2018

Re-using CreateView, UpdateView & friendship

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

--
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/80559ac3-bb8b-429b-9395-7bd09870e636%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment