<?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>DEV Community: weplayinternet</title>
    <description>The latest articles on DEV Community by weplayinternet (@weplayinternet).</description>
    <link>https://dev.to/weplayinternet</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%2F444271%2Fab8cd493-8832-4cb2-a339-37a0c6b37f8b.png</url>
      <title>DEV Community: weplayinternet</title>
      <link>https://dev.to/weplayinternet</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/weplayinternet"/>
    <language>en</language>
    <item>
      <title>Upgrading to Django 3.2 and fixing DEFAULT_AUTO_FIELD warnings </title>
      <dc:creator>weplayinternet</dc:creator>
      <pubDate>Fri, 09 Apr 2021 07:50:28 +0000</pubDate>
      <link>https://dev.to/weplayinternet/upgrading-to-django-3-2-and-fixing-defaultautofield-warnings-518n</link>
      <guid>https://dev.to/weplayinternet/upgrading-to-django-3-2-and-fixing-defaultautofield-warnings-518n</guid>
      <description>&lt;p&gt;When you define a model in Django without specifying a primary key, Django will automatically create a primary key for you. The primary key is set to be an integer. If you want to override the field type, you can do so on a per model basis.&lt;/p&gt;

&lt;p&gt;Starting in Django 3.2, you can now &lt;a href="https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys"&gt;customise&lt;/a&gt; the type of the automatically created primary key in your settings.&lt;/p&gt;

&lt;p&gt;Starting new projects in Django 3.2, the default type for primary keys is set to a &lt;code&gt;BigAutoField&lt;/code&gt; which is a 64 bit integer. However, earlier versions set the type of implicit primary keys to be integers.&lt;/p&gt;

&lt;p&gt;This means that when you upgrade to 3.2, you will start to see warnings about the fact that you do not have an explicitly defined primary key type. Satisfying Django's requirements for an explicitly set primary key type is easy enough, but you also need to choose whether or not you want to upgrade your primary key field types from integer to 64 bit integer.&lt;/p&gt;

&lt;p&gt;Having upgraded to 3.2, the suggestion in the official docs is to run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-Wa&lt;/span&gt; manage.py &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see the warning and the hint to configure a DEFAULT_AUTO_FIELD&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;WARNINGS:
blog.BlogPost: &lt;span class="o"&gt;(&lt;/span&gt;models.W042&lt;span class="o"&gt;)&lt;/span&gt; Auto-created primary key used when not defining a primary key &lt;span class="nb"&gt;type&lt;/span&gt;, by default &lt;span class="s1"&gt;'django.db.models.AutoField'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
    HINT: Configure the DEFAULT_AUTO_FIELD setting or the BlogConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. &lt;span class="s1"&gt;'django.db.models.BigAutoField'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a few ways to fix this. Broadly speaking they fall into two categories - those that do not require a migration and those that do.&lt;/p&gt;

&lt;h2&gt;
  
  
  No migrations please
&lt;/h2&gt;

&lt;p&gt;If you want the simplest upgrade path without migrations, you'll need to tell Django to set the DEFAULT_AUTO_FIELD to &lt;a href="https://docs.djangoproject.com/en/3.2/ref/models/fields/#autofield"&gt;&lt;code&gt;AutoField&lt;/code&gt;&lt;/a&gt; which is, under the hood, an &lt;code&gt;IntegerField&lt;/code&gt;. There are a few places you can do this&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure DEFAULT_AUTO_FIELD in settings
&lt;/h3&gt;

&lt;p&gt;Open your project's &lt;code&gt;settings.py&lt;/code&gt; and add a new line at the bottom of the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DEFAULT_AUTO_FIELD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'django.db.models.AutoField'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting on a per app basis
&lt;/h3&gt;

&lt;p&gt;If you prefer to set your field type on a per app basis rather than for the whole project, you can specify this in &lt;code&gt;apps.py&lt;/code&gt;. You might want to do this if you want a different auto field type per app. For a hypothetical project with a blog app, adding the &lt;code&gt;default_auto_field&lt;/code&gt; line in &lt;code&gt;apps.py&lt;/code&gt; will work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.apps&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AppConfig&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AppConfig&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;default_auto_field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'django.db.models.AutoField'&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'blog'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Migrations
&lt;/h2&gt;

&lt;p&gt;If you don't mind creating and running migrations, then you can embrace the new &lt;code&gt;BigAutoField&lt;/code&gt;. Again, there are a few ways to achieve this:&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure DEFAULT_AUTO_FIELD in settings to use BigAutoField
&lt;/h3&gt;

&lt;p&gt;Open your project's &lt;code&gt;settings.py&lt;/code&gt; and add the following to the bottom:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DEFAULT_AUTO_FIELD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'django.db.models.BigAutoField'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll then need to create and run a migration to update your database to use the new field type. The migrations will alter the id field on your models. To create and run migration in a hypothetical application named &lt;code&gt;blog&lt;/code&gt;, you'd run the following commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; python manage.py makemigrations blog

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; python manage.py sqlmigrate blog &amp;lt;migration_number&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Set default_auto_field to BigAutoField on a per app basis
&lt;/h2&gt;

&lt;p&gt;As above, you can configure the setting on a per app basis, so in &lt;code&gt;apps.py&lt;/code&gt;, you can set the &lt;code&gt;default_auto_field&lt;/code&gt; to &lt;code&gt;BigAutoField&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.apps&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AppConfig&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AppConfig&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;default_auto_field&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'django.db.models.BigAutoField'&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'my_app'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and again create and run migrations for your app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set AutoField or BigAutoField on a per model basis
&lt;/h3&gt;

&lt;p&gt;If you prefer even more fine grained control, you can set a per model id field. A blogging app might have a &lt;code&gt;BlogPost&lt;/code&gt; model. You can explicitly set an id field on each model. &lt;/p&gt;

&lt;p&gt;In a hypothetical blog application you would modify &lt;code&gt;blog/models.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BigAutoField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# ...other fields
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you prefer, you can instead subclass a common base model. Perhaps it saves you some typing if you have a large number of models. In a hypothetical blog application, you would define a common base model and then inherit from it in each model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CommonBaseModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BigAutoField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;abstract&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;


&lt;span class="c1"&gt;# Create your models here.
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CommonBaseModel&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="c1"&gt;# ...other fields
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In either of the two previous examples, you can use &lt;code&gt;AutoField&lt;/code&gt; instead of &lt;code&gt;BigAutoField&lt;/code&gt; if you prefer to keep your primary key as an integer. You still need to make and run migrations to be fully up to date though, because you've changed your model definitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Django 3.2 allows developers to customise the field type used  when creating automatic primary keys. Upgrading means you'll Django will warn you that you do not have the field type set. To silence the warnings, you need to set the default auto field at a project, app, or model level.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>database</category>
    </item>
    <item>
      <title>About the simplest thing a Ruby dev can do with Docker</title>
      <dc:creator>weplayinternet</dc:creator>
      <pubDate>Fri, 31 Jul 2020 08:37:46 +0000</pubDate>
      <link>https://dev.to/weplayinternet/about-the-simplest-thing-a-ruby-dev-can-do-with-docker-2ed6</link>
      <guid>https://dev.to/weplayinternet/about-the-simplest-thing-a-ruby-dev-can-do-with-docker-2ed6</guid>
      <description>&lt;p&gt;Docker, in the beginning, can be overwhelming. Tutorials often focus on creating a complex interaction between Dockerfiles, docker-compose, entrypoint scripts and networking.&lt;/p&gt;

&lt;p&gt;It can take hours to bring up a simple Rails application in Docker and I found that put me off the first few times I tried to play with it.&lt;/p&gt;

&lt;p&gt;I think a rapid feedback loop is essential for playing with a piece of technology.&lt;/p&gt;

&lt;p&gt;If you've never used Docker before, then this is the perfect post for you. I'll start you off on your docker journey and with a few simple commands, you'll be in a Docker container, running ruby interactively.&lt;/p&gt;

&lt;p&gt;You'll need to install Docker. On a Mac, I prefer to install Docker Desktop through homebrew:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew cask &lt;span class="nb"&gt;install &lt;/span&gt;docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're running Linux or Windows, &lt;a href="https://docs.docker.com/engine/install/"&gt;read the official docs for install instructions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;On your Mac, you should now have a Docker icon in your menu bar. Click on it and make sure it says "Docker desktop is running".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L6avFq_l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jfo023rdqhbdbicksqb9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L6avFq_l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/jfo023rdqhbdbicksqb9.png" alt="Docker context menu in Mac menu bar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now open a terminal and type the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;--rm&lt;/span&gt; ruby:2.7.1-buster
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should be dropped into an irb session:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;docker&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;rm&lt;/span&gt; &lt;span class="n"&gt;ruby&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;2.7&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;buster&lt;/span&gt;
&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mo"&gt;001&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello from inside a Docker container"&lt;/span&gt;
&lt;span class="no"&gt;Hello&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="n"&gt;inside&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="no"&gt;Docker&lt;/span&gt; &lt;span class="n"&gt;container&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mo"&gt;002&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should return you to your normal command prompt.&lt;/p&gt;

&lt;p&gt;It doesn't look like you've done much - bringing up an irb session isn't groundbreaking. You can do the same by typing irb at your usual prompt.&lt;/p&gt;

&lt;p&gt;Breaking down the command used:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run&lt;/code&gt; runs a command in a new container&lt;/p&gt;

&lt;p&gt;&lt;code&gt;-it&lt;/code&gt; these two options that tell docker to run in interactive mode and to allocate a pseudo-TTY&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--rm&lt;/code&gt; instructs docker to automatically delete the container when we exit&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ruby:2.7.1-buster&lt;/code&gt; theimage that we want to run.&lt;/p&gt;

&lt;p&gt;You'll notice that none of those commands actually tried to execute irb to run the Ruby REPL. So how did we end up in an irb session?&lt;/p&gt;

&lt;p&gt;The answer is that the Docker image we used defined a default step for us to start irb.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>ruby</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
