3đź‘Ť
It’s a coding style. “Object” in OOP is data and methods, together. The object has everything you need to hold the data and manipulate it. There is no “right” answer, more opinion and style.
So you can write:
r = Reservation.objects.get(pk=1)
r.get_client_reservation()
Rather then:
from . import get_client_reservation
get_client_reservation(r)
But the truth is that Python modules are a very good solution to keep things together, and it’s easier to debug than a complex inheritance chain.
In django the OOP is essential because the framework lets you easily subclass components and customise only what you need, this is hard to do without objects.
If you need a specific form, with specific fields, then you can write it as a simple module with functions. But if you need a generic “Form” that everybody can customise (or a model, authentication backend etc), you need OOP.
So bottom line (IMHO): if Reservation is at the bottom of the pyramid, the end line of data and code, no big difference, more personal preference. If it’s in the top and you are going to need ReservationThis and ReservationThat, OOP is better.
1đź‘Ť
This isn’t a technical answer, but try doing a git blame on that code, and seeing who wrote the methods, and ask them why they chose to do it like that. In general it’s better to keep the methods on the class (for multiple reasons) – for example being able to do dir(r) (where r is a reservation) and seeing all the methods on r. There may be a reason though (that we can’t know unless we saw the code)
- [Django]-How to manage views,urls and models on large scale in Django?
- [Django]-Django child url does not show its template
- [Django]-How can i split up a large django-celery tasks.py module into smaller chunks?
- [Django]-Django Forbidden HttpResponse in Test-Drive
0đź‘Ť
You shoud put a method inside a class if the it’s related with the class, for example if it needs some class variable or if it logically belongs with the class
- [Django]-Django Celery – How to start a task with a delay of n – seconds – countdown flag is ignored
- [Django]-Django modify bridge table