Clarify usage of as_view kwargs for setting arguments on class based views #314

Closed
wants to merge 2 commits into
from

Projects

None yet

3 participants

@dmclain

The current documentation could lead to a new Python developer thinking that passing a keyword argument into as_view will result in an attribute being specified and set to the correct value. The current error message doesn't provide any additional information that might help the user towards a solution.

@mjtamlyn mjtamlyn and 1 other commented on an outdated diff Sep 3, 2012
docs/ref/class-based-views/index.txt
instance that is used to service a request. Using the previous example,
this means that every request on ``MyView`` is able to use ``self.size``.
+Arguments must correspond to attributes that can be set on the class using
+``setattr``.
@mjtamlyn
mjtamlyn Sep 3, 2012

I don't think this is right - you can setattr any argument on a view, but it has to be getattr-able first.

@dmclain
dmclain Sep 3, 2012

Technically, the attribue must be getattr-able first and then seattr-able afterwards. A read-only attribute would not be settable via passing an argument to as_view. I suppose it's most accurate to say that setting an attribute via a keyword argument to as_view will only work if hasattr(YourView, 'keyword_argument_name') returns True and that attribute is settable via setattr. getattr is not a sufficient condition because of the existence of read only properties. Can you have a Python object with a write-only attribute?

@dmclain
dmclain Sep 3, 2012

And apparently 2 years of developing Python still isn't enough to fully grok the way attributes work. You're right, getattr should be mentioned.

@timgraham
Django member

I've merged this, thanks!

@timgraham timgraham closed this Nov 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment