<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Postgresql on Greg Kaleka</title><link>https://gregkaleka.com/tags/postgresql/</link><description>Recent content in Postgresql on Greg Kaleka</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 31 Jan 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://gregkaleka.com/tags/postgresql/index.xml" rel="self" type="application/rss+xml"/><item><title>Splitting strings with Postgres and Django</title><link>https://gregkaleka.com/blog/splitting-strings-postgres-django/</link><pubDate>Tue, 31 Jan 2023 00:00:00 +0000</pubDate><guid>https://gregkaleka.com/blog/splitting-strings-postgres-django/</guid><description>&lt;p>The Django docs give an example of querying an &lt;code>ArrayField&lt;/code> with some really powerful lookups.
But what if we inherited a project, and the prior developer didn&amp;rsquo;t know about this Postgres-specific
field type?&lt;/p>
&lt;p>Imagine we have this version of the &lt;code>Post&lt;/code> model, with a simple &lt;code>CharField&lt;/code> for the tags&amp;hellip;&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Post&lt;/span>(models&lt;span style="color:#f92672">.&lt;/span>Model):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> name &lt;span style="color:#f92672">=&lt;/span> models&lt;span style="color:#f92672">.&lt;/span>CharField(max_length&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">200&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> tags &lt;span style="color:#f92672">=&lt;/span> models&lt;span style="color:#f92672">.&lt;/span>CharField(max_length&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#ae81ff">200&lt;/span>, blank&lt;span style="color:#f92672">=&lt;/span>&lt;span style="color:#66d9ef">True&lt;/span>) &lt;span style="color:#75715e"># not ArrayField 😞&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">def&lt;/span> __str__(self):
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> self&lt;span style="color:#f92672">.&lt;/span>name
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&amp;hellip;and by convention, the tags are filled out with a pipe separator, like this:
&lt;code>thoughts|django&lt;/code>. It turns out we can almost query this field the same way as is done
in the docs, but there&amp;rsquo;s a gotcha:&lt;/p></description></item></channel></rss>