1👍
Think of a Django view as a verb and a Django model as a noun. The method on a Django model is an adjective and specifically relates to an instance of the model (though some methods can be used to tell Django things about the class itself).
As an analogy, consider a model Car
with an attribute year
that tells us the year in which an individual member of Car
was built. In some states, for inspection purposes, a “new” car is any car that is less than two or three years old. A “new” car may cost less to inspect, since you don’t need to do emissions tests.
Here, “new” is an adjective that describes an instance of the Car
class. If we wanted to have a function is_new
that tells us whether the car can be considered “new” or not, we would attach that to the model. We could then do car = Car(<car info here>)
and then do car.is_new()
to find out if we can save on our inspections.
3👍
Your misunderstanding is here:
Models are the instructions for how database tables should be created
That is one part of a model, certainly, but far from the only part. Models are the fundamental data objects of your application, and the whole point of doing object-orientated programming – rather than querying the database directly and passing around data rows – is that these objects are rich. They contain methods that deal with the data they contain; was_published_recently
is a great example of the kind of thing a model method should do.
- [Django]-Model inheritance in django-nonrel on app engine
- [Django]-Django-smart-selects doesn't work in templates