Django - filter model
<p>I have a model with a field that is a list.
For example:</p>

<pre><code>mymodel.sessions = [session1, session2]
</code></pre>

<p>I need a query to get all <code>mymodels</code> that <code>session1</code> is exist their sessions.</p>

<p>The model`s field looks like that</p>

<pre><code>sessions = models.ForeignKey("Session", related_name="abstracts",
                                null=True, blank=True)
</code></pre>

<p>Thank you !</p>

<p>From the <a href="https://docs.djangoproject.com/en/1.10/ref/models/querysets/#django.db.models.query.QuerySet.filter" rel="nofollow">Django docs for <code>filter</code></a>:</p>

<blockquote>
  <p><code>filter(**kwargs)</code></p>
  
  <p>Returns a new QuerySet containing objects that match the given lookup parameters.</p>
</blockquote>

<p>You can filter on a ForeignKey relationship using the id, if you have it:</p>

<blockquote>
  <p>The field specified in a lookup has to be the name of a model field. Thereâs one exception though, in case of a ForeignKey you can specify the field name suffixed with _id. In this case, the value parameter is expected to contain the raw value of the foreign modelâs primary key.</p>
</blockquote>

<p>In your instance, this would like the following:</p>

<pre><code>mymodel.objects.filter(sessions_id=4)
</code></pre>

<p>If you want to filter on any other field in the Sessions model, simply use a double underscore with the field. Some examples:</p>

<pre><code>mymodel.objects.filter(sessions__name='session1')
mymodel.objects.filter(sessions__name__contains='1')
</code></pre>

python django django-models