Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the relation field of a related object in Django
    primarykey
    data
    text
    <p>Say I have those two model:</p> <pre><code>class Project(models.Model): name = models.CharField('Name', max_length=250) class ProjectUpdate(models.Model): project = models.ForeignKey(Project) title = models.CharField('Title', max_length=250) </code></pre> <p>Let's say that in my code I work with a Project instance, but I can't assume the object I'm working with is a project instance. I can only assume that it has one or more related models on it which I need to work on.</p> <p>Now I manage to get the related class name using the related manager (project_obj.projectupdate_set). But what I need now is to know which field creates the relation. In this case this would be the "project" field on ProjectUpdate.</p> <p>The only place where I find a reference to it is in the <code>core_filters</code> property of the related manager which gives me: {'project__id': 1}</p> <p>I probably could use this, but it doesn't feel right.</p> <p>Any idea ?</p> <p><strong>Edit</strong>:</p> <p>Here's the properties of the RelatedManager:</p> <pre><code>['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_copy_to_model', '_db', '_inherited', '_insert', '_set_creation_counter', '_update', 'add', 'aggregate', 'all', 'annotate', 'bulk_create', 'clear', 'complex_filter', 'contribute_to_class', 'core_filters', 'count', 'create', 'creation_counter', 'dates', 'db', 'db_manager', 'defer', 'distinct', 'exclude', 'exists', 'extra', 'filter', 'get', 'get_empty_query_set', 'get_or_create', 'get_prefetch_query_set', 'get_query_set', 'in_bulk', 'instance', 'iterator', 'latest', 'model', 'none', 'only', 'order_by', 'prefetch_related', 'raw', 'remove', 'reverse', 'select_for_update', 'select_related', 'update', 'using', 'values', 'values_list'] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload