[Python] Django built-in class based view - Where is self.object from?

Discussão em 'Python' iniciado por Stack, Junho 11, 2025.

  1. Stack

    Stack Membro Participativo

    In the following code, I don't understand something about the object.

    class SingleObjectMixin(ContextMixin):
    """
    Provide the ability to retrieve a single object for further manipulation.
    """
    model = None
    queryset = None
    slug_field = 'slug'
    context_object_name = None
    slug_url_kwarg = 'slug'
    pk_url_kwarg = 'pk'
    query_pk_and_slug = False

    ...

    ...


    def get_context_data(self, **kwargs):
    """Insert the single object into the context dict."""
    context = {}
    if self.object:
    context['object'] = self.object
    context_object_name = self.get_context_object_name(self.object)
    if context_object_name:
    context[context_object_name] = self.object
    context.update(kwargs)
    return super().get_context_data(**context)


    There is an attribute self.object in the method get_context_data. I would like to figure out where the attribute object is created from.

    Continue reading...

Compartilhe esta Página