<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Fabio Caccamo</title>
    <description>The latest articles on Forem by Fabio Caccamo (@fabiocaccamo).</description>
    <link>https://forem.com/fabiocaccamo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F239504%2F10dacb0e-3bbb-46cd-84ef-27d8d3391b0a.jpg</url>
      <title>Forem: Fabio Caccamo</title>
      <link>https://forem.com/fabiocaccamo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fabiocaccamo"/>
    <language>en</language>
    <item>
      <title>How to use Emojis in a Python + Django + MySQL project</title>
      <dc:creator>Fabio Caccamo</dc:creator>
      <pubDate>Tue, 30 May 2023 12:30:00 +0000</pubDate>
      <link>https://forem.com/fabiocaccamo/how-to-use-emojis-in-a-python-django-mysql-project-1880</link>
      <guid>https://forem.com/fabiocaccamo/how-to-use-emojis-in-a-python-django-mysql-project-1880</guid>
      <description>&lt;p&gt;Did you ever received an error like this while saving some text containing Emojis to the database?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DataError at /admin/myapp/mymodel/1/change/
(1366, "Incorrect string value: '\\xF0\\x9F\\x99...' for column 'text' at row 1")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To overcome this problem you have to change the database and application charset to &lt;code&gt;utf8mb4&lt;/code&gt;:&lt;/p&gt;

&lt;h2&gt;
  
  
  MySQL
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Backup your database&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit &lt;code&gt;MySQL&lt;/code&gt; conf and add/set the charset settings:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nano /etc/mysql/mysql.conf.d/mysqld.cnf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#
# * Character Set
#
character_set_server = utf8mb4
collation_server = utf8mb4_unicode_ci
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Update database tables character-set to &lt;code&gt;utf8mb4&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT CONCAT('ALTER TABLE ', table_name, ' CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;') AS alter_statement
FROM information_schema.tables
WHERE table_schema = 'mydatabasename';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Run the generated &lt;code&gt;ALTER&lt;/code&gt; statements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Restart &lt;code&gt;MySQL&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/etc/init.d/mysql restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Django
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add the &lt;code&gt;charset&lt;/code&gt; option with &lt;code&gt;utf8mb4&lt;/code&gt; value to your database settings:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        # ...
        "OPTIONS": {
            "charset": "utf8mb4",
        },
    },
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Restart your application server&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Done, now you can store Emojis in your database!&lt;/em&gt; 🚀 &lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>mysql</category>
      <category>emoji</category>
    </item>
    <item>
      <title>How to improve the default django admin</title>
      <dc:creator>Fabio Caccamo</dc:creator>
      <pubDate>Tue, 29 Oct 2019 17:43:39 +0000</pubDate>
      <link>https://forem.com/fabiocaccamo/how-to-improve-the-default-django-admin-4e83</link>
      <guid>https://forem.com/fabiocaccamo/how-to-improve-the-default-django-admin-4e83</guid>
      <description>&lt;p&gt;I work on a daily basis with python/django since 9 years now and I like it very much. &lt;/p&gt;

&lt;p&gt;One of the best aspects of working with django is its admin, it is very simple to use (also for clients), but it lacks in customization. &lt;/p&gt;

&lt;p&gt;Before giving backend access to the clients I always did some customization to the admin &lt;em&gt;(like changing the logo and the main colors)&lt;/em&gt; to match the client identity.&lt;/p&gt;

&lt;p&gt;It was boring and repetitive work, so I decided to write a library to do it.&lt;/p&gt;

&lt;p&gt;The result is the &lt;strong&gt;django-admin-interface&lt;/strong&gt; library and these are the main features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Themes management and customization (title, logo and colors)&lt;/li&gt;
&lt;li&gt; Related popup-windows replaced by modals&lt;/li&gt;
&lt;li&gt; Environment name/marker&lt;/li&gt;
&lt;li&gt; Language chooser&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is open-source on GitHub a lot of people use it:&lt;br&gt;
&lt;a href="https://github.com/fabiocaccamo/django-admin-interface" rel="noopener noreferrer"&gt;https://github.com/fabiocaccamo/django-admin-interface&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzqod2wxsnctk3fi8lddv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fzqod2wxsnctk3fi8lddv.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>admin</category>
      <category>showdev</category>
    </item>
    <item>
      <title>python-benedict: dict subclass with keypath support, standardized IO shortcuts and many utilities.</title>
      <dc:creator>Fabio Caccamo</dc:creator>
      <pubDate>Mon, 21 Oct 2019 08:20:28 +0000</pubDate>
      <link>https://forem.com/fabiocaccamo/python-benedict-dict-subclass-with-keypath-support-standardized-io-shortcuts-and-many-utilities-50k2</link>
      <guid>https://forem.com/fabiocaccamo/python-benedict-dict-subclass-with-keypath-support-standardized-io-shortcuts-and-many-utilities-50k2</guid>
      <description>&lt;p&gt;Recently I was involved in a project were I had to &lt;strong&gt;load data from different providers&lt;/strong&gt;, each one providing data in a different format, &lt;code&gt;JSON&lt;/code&gt; in the best case but even in &lt;code&gt;XML&lt;/code&gt; or &lt;code&gt;YAML&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Received data was dirty and pretty far from being the data you would like to receive, for example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Extra white space in values&lt;/li&gt;
&lt;li&gt; Boolean values expressed using words like &lt;code&gt;Yes&lt;/code&gt; or &lt;code&gt;No&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; Datetime values always expressed in different formats&lt;/li&gt;
&lt;li&gt; Keys didn't respect a standard naming convention like &lt;code&gt;snake_case&lt;/code&gt; or &lt;code&gt;camelCase&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; Unexpected data structure changes without any warning by the provider&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To wake me up from this nightmare I decided to normalize and simplify the whole process reducing unexpected errors near to zero.&lt;/p&gt;

&lt;p&gt;The abstract problem was always the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Reading/writing&lt;/strong&gt; data from/to different formats in a standard way&lt;/li&gt;
&lt;li&gt; Accessing nested data values quickly, using &lt;strong&gt;keypath&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; Get data values trying to &lt;strong&gt;parse&lt;/strong&gt; them in the expected type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;I decided to write my own library.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;code&gt;python-benedict&lt;/code&gt; is a &lt;code&gt;dict&lt;/code&gt; subclass with &lt;strong&gt;keypath&lt;/strong&gt; support, &lt;strong&gt;I/O&lt;/strong&gt; shortcuts (&lt;code&gt;Base64&lt;/code&gt;, &lt;code&gt;JSON&lt;/code&gt;, &lt;code&gt;TOML&lt;/code&gt;, &lt;code&gt;XML&lt;/code&gt;, &lt;code&gt;YAML&lt;/code&gt;, &lt;code&gt;query-string&lt;/code&gt;) and &lt;strong&gt;many utility methods&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's &lt;strong&gt;open-source&lt;/strong&gt; on GitHub:&lt;br&gt;
&lt;a href="https://github.com/fabiocaccamo/python-benedict"&gt;https://github.com/fabiocaccamo/python-benedict&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check it out, any feedback is appreciated.&lt;br&gt;
Thanks&lt;/p&gt;

</description>
      <category>python</category>
      <category>opensource</category>
      <category>showdev</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
