title: django postgresql opposite list search
body: <p>Good afternoon</p>
  
  <p>I am using django with postgresql for an application.</p>
  
  <p>I have a model that contains a field which is a charfield and stores <em>none/one/many</em> references to external databases.</p>
  
  <ul>
  <li>If <em>none</em> it is empty</li>
  <li>If <em>one</em> it is the reference itself ("CCCC")</li>
  <li>If <em>many</em> it is each reference separated by semicolons ("AAAA;BBBB;CCCC;DDDD;EEEE;"). This can be seen below:</li>
  </ul>
  
  <p>An example of the python file of the model can be found below:</p>
  
  <pre><code>Models.py file:
  
  class Data(models.Model):
      ...
      external_database_id = models.CharField(max_length=255)
      ...
  </code></pre>
  
  <p>The reason for using a charfield was that, at the beggining, all this information was only to display it to the user, and no other option was applied on it.</p>
  
  <p>Now I need to implement/apply a lookup filter with django. I have an array containing some references:</p>
  
  <pre><code>For example: [AAAA,BBBB,EEEE]
  </code></pre>
  
  <p>I need to know if the any entry on the database matches to any of the entries of this list</p>
  
  <p>So far I have tried the "external_database_id__in" lookup filter "<a href="https://docs.djangoproject.com/en/1.11/ref/models/querysets/#in" rel="nofollow noreferrer">https://docs.djangoproject.com/en/1.11/ref/models/querysets/#in</a>". It works in the case of the "<em>none</em>" and "<em>one</em>" but not in the "<em>many</em>", since there is no match for the whole string to the list...</p>
  
  <ul>
  <li><em>none</em>: "" in [AAAA,BBBB,EEEE] --> FALSE (as expected)</li>
  <li><em>one</em>: "CCCC" in [AAAA,BBBB,EEEE] --> FALSE (as expected)</li>
  <li><em>one</em>: "EEEE" in [AAAA,BBBB,EEEE] --> TRUE (as expected)</li>
  <li><em>many</em>: "AAAA;BBBB;" in [AAAA,BBBB,EEEE] --> FALSE (as expected, but undesired, since TRUE would be the correct answer)</li>
  <li><em>many</em>: "AAAA;CCCC;" in [AAAA,BBBB,EEEE] --> FALSE (as expected, but undesired, since TRUE would be the correct answer)</li>
  <li><em>many</em>: "CCCC;DDDD;" in [AAAA,BBBB,EEEE] --> FALSE (as expected)</li>
  </ul>
  
  <p>I have thought to do the opposite, but couldn't find the way to do it in django or postgresql:</p>
  
  <ul>
  <li><em>none</em>: any[AAAA,BBBB,EEEE] in "" --> FALSE</li>
  <li><em>one</em>: any[AAAA,BBBB,EEEE] in "CCCC" --> FALSE</li>
  <li><em>one</em>: any[AAAA,BBBB,EEEE] in "EEEE" --> TRUE</li>
  <li><em>many</em>: any[AAAA,BBBB,EEEE] in "AAAA;BBBB;" --> TRUE</li>
  <li><em>many</em>: any[AAAA,BBBB,EEEE] in "AAAA;CCCC;" --> TRUE</li>
  <li><em>many</em>: any[AAAA,BBBB,EEEE] in "CCCC;DDDD;" --> FALSE</li>
  </ul>
  
  <p>Any ideas?
  Thank you very much!</p>