2

I'm trying to refactor some (4 in number) ajax views which work almost same as the following procedure:

  1. Get some (2-4 in number) of objects using args
  2. Return a json if any of the objects were not found, else continue
  3. Then we extract another object (using the objects found in step 1) to update and create one if not found
  4. Create a new form depending on the type of the object we got in step 3 and validate and update the object
  5. Return a json in the end depending on form validity

The first 2 steps are common in all 4 ajax views. Even the models from which they try to extract the objects using args is common.

However, step 3 onwards things are very distinct in nature. The model used to extract the object and in turn the form used for validation. Also depending on the model there are certain more stuff happening in 2 of those ajax-views.

I'm quite new to Class-based Views and read this and this. Yet, I'm unable to see if I'd be making a drastically convenient future in terms of maintenance if I convert those 4 ajax-views to CBVs. And yeah, there's possibility that we create another such ajax-view once every year.

Question is: Should I use CBV or not, given that I can only stop replication of first 2 steps if I used CBV?

Community
  • 1
  • 1
Ravi Ojha
  • 722
  • 1
  • 6
  • 15
  • Are you talking about class based views or class based generic views? [Class-based Views: Past, Present and Future video from Russell Keith-Magee](https://youtu.be/8_x6qLfZjjs) – Sayse Jul 06 '16 at 14:18
  • @Sayse Edited the last line to not confuse between CBV and CBGV. Thanks for that link. I feel I need more understanding of CBV to realize its beauty. :) – Ravi Ojha Jul 06 '16 at 14:20
  • 1
    The video above does a pretty good explanation.. – Sayse Jul 06 '16 at 14:21
  • 1
    I'd say make a common base view that calls methods that implement step 1-5, and then make subclasses that implement 1-5 differently where needed. – RemcoGerlich Jul 06 '16 at 14:21

1 Answers1

2

Personally I find class based views easier to read. Ability to use inheritance to not repeat the same code is nice even i you use it just a little bit. I find that the class based views really shine when you develop RESTful APIs, since you can process all the different request methods in the same class.

Mad Wombat
  • 14,490
  • 14
  • 73
  • 109