Missing attributes self.object and self.object_list
Closed this issue · 3 comments
Hello, I've just discovered this, thanks so much!
I've noticed that some generic classes are missing some attributes or probably I'm not looking in the correct place.
For example a DetailView
While this view is executing,
self.object
will contain the object that the view is operating upon.
Or for UpdateView
When using UpdateView you have access to self.object, which is the object being updated.
For ListView
While this view is executing,
self.object_list
will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.
And so on.
But self.object
or self.object_list
are not listed in the ccbv site.
I am missing something?
Hi Leonardo
Thanks for bringing this up, and you're right: those attributes aren't present in our documentation of the classes. The reason is that, unlike those that are included, these attributes are set during the execution of the view, not defined on the class (the latter being what we inspect).
With regard to including them, this also makes it tricky for a couple of reasons:
- We would have to parse the code in some other way than we currently do, or otherwise run the view and make sure we hit all the code paths. These both sound daunting and pretty unappealing.
- I'm not sure how one would go about expressing this information, even if we could capture it. From your second example,
self.object
is set to the value ofself.get_object()
inget()
andpost()
, but to the value ofform.save()
inform_valid()
. If we knew this, I feel like, at best, we could express thatobject
may in some situations be an attribute, but I'm not certain if that's of much use.
The docs themselves benefit from the authors editorialising (hence being able to point out these attributes) but with ccbv just representing what's in the code directly, I think this is a harder problem to solve. I've always found having access to the source on the ccbv pages to be enough in these situations, but if there's more we can do that'd be great. If you have any thoughts on how this could work, or just on my explanation, I'd love to hear them.
Hello @exonian thanks for your time and your explanation.
At this point I have no other way to suggest how to fix this other than hardcoding these attributes manually, but I'm not sure if that's a good fit for this project 🤔 That would probably require manual updates if things will ever change
I'm going to close this. There's no way to handle dynamic class attributes automatically, and there's an important difference anyway - only things previously set on the class can be set using MyView(attribute=myvar)
.