Is there a established way that i validate an object in the dispatch without making an extra database call when
self.get_object()
is called later in get/post?Here is what i have so far (slightly altered for this question):
class CourseUpdateView(UpdateView): def dispatch(self, request, *args, **kwargs): self.request = request self.kwargs = kwargs self.object = self.get_object() if self.object.is_online: messages.warning(request, "Sorry this one can't be updated") return redirect("course:detail", pk=self.kwargs['pk']) # this is going to call self.get_object again isn't it? return UpdateView.dispatch(self, request, *args, **kwargs)
score:7
Accepted answer
You can cache the result of get_object()
.
Here's a trivial example:
class CourseUpdateView(UpdateView):
# [...] your dispatch method
def get_object(self):
# it doesn't matter how many times get_object is called per request
# it should not do more than one request
if not hasattr(self, '_object'):
self._object = super(CourseUpdateView, self).get_object()
return self._object
Credit To: stackoverflow.com
Related Query
- Django Class Based View: Validate object in dispatch
- Sending request.user object to ModelForm from class based generic view in Django
- how to dispatch a specific method with csrf_exempt in django class based view
- Manually dispatch a Django Class Based View
- Apply Decorator in Class Based View Django according to object value
- 'tuple' object has no attribute 'get' when requesting class based view Django
- Validate Uniqueness Fields in Django Class Based View
- What is the right way to validate if an object exists in a django view without returning 404?
- Django - Class Based Generic View - "No URL to redirect to"
- Django Class Based View for both Create and Update
- Accessing request.user in class based generic view CreateView in order to set FK field in Django
- reverse for success_url on Django Class Based View complain about circular import
- Django Call Class based view from another class based view
- Django class based view ListView with form
- Django: a class based view with mixins and dispatch method
- Django class based post-only view
- Manually calling a class based generic view in Django
- Function decorators with parameters on a class based view in Django
- How to specify a custom 404 view for Django using Class Based Views?
- How to access current user in Django class based view
- Django: model object "has no attribute '_meta'" in class based view
- Django Class Based View Composite
- Check view method parameter name in Django class based views
- django how to assert url pattern resolves to correct class based view function
- Django handler500 as a Class Based View
- Using modelformset_factory in a Django Class Based View
- Django reverse url with parameters to a class based view
- django Initialising Choices in Form from Class Based View
- Filtering Objects in Class based view Django using Query parameters?
- django rest framework - cannot get the class based view right
More Query from same tag
- Django rest api - searching a method field with the search filter
- How to use filter for field in django-admin
- Django media files doesn't work when debug=false
- How to access the Azure AD Groups and user details using python?
- Django DRF Token Authentication
- Syntax error while trying to install Django
- How to accept localized date format (e.g dd/mm/yy) in a DateField on an admin form?
- How to dispose figure in matplotlib
- UNIQUE constraint failed
- Date ValidationError in Django
- Django-Rest-Framework endpoint not accessing my database collection
- Django: Cannot access sessionid cookie using Javascript
- Django Broken Pipe Error
- Django: Quickly deal with adding non-nullable field
- Creating a User Account For Every Hospital Instance Created - Django