[Answer]-Django Tutorial: Where does the other objects come from?


Django does ForeignKey lookups to establish relations between related objects.
These relations are added dynamically as accessors suffixed by _set

def get_accessor_name(self):
    # This method encapsulates the logic that decides what name to give an
    # accessor descriptor that retrieves related many-to-one or
    # many-to-many objects. It uses the lower-cased object_name + "_set",
    # but this can be overridden with the "related_name" option.
    if self.field.rel.multiple:
        # If this is a symmetrical m2m relation on self, there is no reverse accessor.
        if getattr(self.field.rel, 'symmetrical', False) and self.model == self.parent_model:
            return None
        return self.field.rel.related_name or (self.opts.object_name.lower() + '_set')
        return self.field.rel.related_name or (self.opts.object_name.lower())

Code in Django sources that does this (models/related.py)

Leave a comment