get_absolute_url

One more thing that we are seeing for the first time in the Car model is get_absolute_url. There is nothing special about the implementation. It's just a class method that returns a URL, which it builds using the reverse function and the primary key of the object. This isn't anything new. We have been creating URLs like this for detail pages since the first chapter. What's interesting to note here is that Django assigns special meaning to the get_absolute_url method on a model class. There are a number of places where Django will automatically use the return value of the get_absolute_url method if the method exists on a model object. For instance, the CreateView generic method uses it. If you do not provide a success_url attribute and a custom get_success_url method on the view class, Django will try to get the URL to redirect from the get_absolute_url method on the newly created object if the method is defined in the model class.

Django also uses this method in the admin app, as we'll see later on. If you are interested, you can take a look at the documentation for it:

https://docs.djangoproject.com/en/stable/ref/models/instances/#get-absolute-url/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.188.138