set `object = None` on `SingleObjectMixin`

69 views
Skip to first unread message

steve byerly

unread,
Aug 14, 2015, 3:38:58 PM8/14/15
to Django developers (Contributions to Django itself)
In the `SingleObjectMixin` generic view mixin, the `get_context_data` method checks if the object is set by using: `if self.object`

I often include this mixin just for the `get_object` functionality and error handing it brings. I end up having to set `object = foo` or `object = None` to suppress the AttributeError

It seems like the class should initialize `self.object = None` instead of relying on a derived class setting it, especially because it doesn't guarantee that it will be added to the context - it only adds 'object' (and whatever the `context_object_name` param is) to the context if there's an object to add.

I'm sure there has been discussion on this before, but I was unable to find it here/trac.

Tim Graham

unread,
Aug 14, 2015, 5:38:59 PM8/14/15
to Django developers (Contributions to Django itself)

steve byerly

unread,
Aug 15, 2015, 1:24:01 AM8/15/15
to Django developers (Contributions to Django itself)
I still haven't seen a definitive reason why the mixin doesn't do the check for the object, other than: 'Personally, I don't like this change'

Shouldn't the mixin stand on its own and not require going to the django docs to find the dependency that the user needs to set self.object in get/post/etc? This same class even does checks for other attributes - e.g. raising an exception if the model class isn't set. It also sets `queryset = None`, so why wouldn't it do the same for `object`?
Reply all
Reply to author
Forward
0 new messages