<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bharath Kallur on Medium]]></title>
        <description><![CDATA[Stories by Bharath Kallur on Medium]]></description>
        <link>https://medium.com/@bkallur21?source=rss-412ea78925c6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oHvfLAKQlmGqRd_xBI3dSA.jpeg</url>
            <title>Stories by Bharath Kallur on Medium</title>
            <link>https://medium.com/@bkallur21?source=rss-412ea78925c6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 21:43:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bkallur21/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Nested Relationships in Serializers for OneToOne fields in Django Rest Framework]]></title>
            <link>https://medium.com/free-code-camp/nested-relationships-in-serializers-for-onetoone-fields-in-django-rest-framework-bdb4720d81e6?source=rss-412ea78925c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/bdb4720d81e6</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Bharath Kallur]]></dc:creator>
            <pubDate>Sun, 08 Oct 2017 16:39:22 GMT</pubDate>
            <atom:updated>2017-12-16T15:41:17.031Z</atom:updated>
            <content:encoded><![CDATA[<p>The Django Rest Framework (<a href="http://www.django-rest-framework.org/">DRF</a>) is one of the effectively written frameworks around Django and helps build REST APIs for an application back-end.</p><p>I was using it in one of my personal projects and stumbled upon this challenge of “serializing a model which is referencing another model via <a href="https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.OneToOneField">OneToOne field</a>.”</p><p>`I was using the <strong>User </strong>model from <a href="https://docs.djangoproject.com/en/2.0/ref/contrib/auth/">django.contrib.auth.models</a>. I wanted to write an API to create and update a user object through a single API which also updates my model’s attributes. The solution was to use DRF’s <strong>Nested Relationships </strong>in serialization.</p><p>I shall assume you have a fair working knowledge of Python, virtualenv, pip, Django and DRF before proceeding. If not, please learn more and feel free to return if you are ever stuck on nested relationships in serialization.</p><p>The example I am considering here is a <strong>University Student</strong> model, referencing <strong>User </strong>model via the OneToOne field. My goal is a single API for creating and getting user details like name, username, and email along with a student attribute such as subject major.</p><p>Here is how my <strong>models.py</strong> looks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa07217879380bda4958a22a36d090b2/href">https://medium.com/media/fa07217879380bda4958a22a36d090b2/href</a></iframe><p>Next, the serializer for the above model determines the attributes to manipulate. If you observe below, I have 2 serializer classes, <strong>UserSerializer </strong>and StudentSerializer.<strong> </strong>This is our point of interest.</p><p>I have declared a user attribute which is a serializer field here. That user attribute will primarily hold the entire reference for the UserSerializer class. In the <strong>fields</strong> of StudentSerializer , we just see ‘user’ and ‘subject_major’. This allows us to enter the student (or user) attributes along with the subject_major.</p><p>A user entry is created which is referenced by the student entry. We override the create method of StudentSerializer to create a user object first, and use that to create the student object.</p><p>The <strong>serializer.py </strong>is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/400e17d513f7ce0c84540348a3db16f9/href">https://medium.com/media/400e17d513f7ce0c84540348a3db16f9/href</a></iframe><p>The <strong>views.py</strong> should be quite straightforward if you are already familiar with the <a href="https://docs.djangoproject.com/en/1.11/topics/class-based-views/">class-based views </a>of Django. We will be using the serializer to validate and create the model objects:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b0eb48a05dde0ff91f99f524b4591d9/href">https://medium.com/media/5b0eb48a05dde0ff91f99f524b4591d9/href</a></iframe><p>I have included /univstud/ url for achieving post and get requests for university student.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbe09d6c64cd63df980819d0a6f5f990/href">https://medium.com/media/cbe09d6c64cd63df980819d0a6f5f990/href</a></iframe><p>The <strong>POST</strong> request call would look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*AxmyhgMuhoFfved4pSDUhw.png" /><figcaption>post call for /univstud/</figcaption></figure><p>The <strong>Get</strong> request call would look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*FGXTNU4Fri9iTO2ab1mRqA.png" /><figcaption>get call for /univstud/</figcaption></figure><p>That’s all!:)</p><p>Nested Relationship is thus enabled on <strong>StudentSerializer</strong> to reference user.</p><p>Complete code is in <a href="https://gitlab.com/bharathkallurs/writeup/tree/master">my gitlab</a> repository.</p><h3>References:</h3><ol><li><a href="http://www.django-rest-framework.org/api-guide/relations/">http://www.django-rest-framework.org/api-guide/relations/</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdb4720d81e6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/nested-relationships-in-serializers-for-onetoone-fields-in-django-rest-framework-bdb4720d81e6">Nested Relationships in Serializers for OneToOne fields in Django Rest Framework</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up CI/CD on GitLab for deploying Python Flask application on Heroku]]></title>
            <link>https://medium.com/free-code-camp/setting-up-a-ci-cd-on-gitlab-for-deploying-a-python-flask-application-on-heroku-e154db93952b?source=rss-412ea78925c6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e154db93952b</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[Bharath Kallur]]></dc:creator>
            <pubDate>Thu, 13 Apr 2017 04:31:28 GMT</pubDate>
            <atom:updated>2017-12-08T21:13:35.852Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I came across a challenge to deploy a Python Flask web application to Heroku. The code of the App was hosted in GitLab.</p><p>Heroku supports deploying an App from GitHub, Dropbox along with the usual Heroku git. It had been quite a while since I used Heroku. I was wondering if I can deploy code directly from my GitLab repository instead of using any of the sources mentioned above.</p><p>I couldn’t find any information or documentation around deploying applications hosted in a GitLab repository to <a href="https://devcenter.heroku.com/categories/deployment">Heroku</a>. I browsed a bit on GitLab and it turned out that apart from helping test and build your project, GitLab CI can also help <a href="https://docs.gitlab.com/ce/ci/environments.html">deploy</a> you App to your hosting infrastructure. I was now intrigued.</p><p>Before I delve into how I deployed the App, I’d like to explain the benefits of using GitLab or GitHub when you can easily get things done with Heroku Git.</p><ol><li><strong>Easier code maintenance </strong>- With code repository hosting services like GitHub and GitLab, maintenance of code is easy.</li><li><strong>Customising pipelines </strong>- With GitLab, we can write our own <a href="http://yaml.org/">yaml</a> file and include the libraries required for running our application.</li><li><strong>For better understanding of Continuous Integration and Continuous Development (CI/CD)</strong> - For beginners, this setup helps you understand the coding workflow of testing -&gt; version control -&gt; code maintenance -&gt; application deployment.</li></ol><p>Here are the steps required to deploy your App hosted in GitLab to Heroku. The steps here assume you already have a good understanding of Python, Flask, version control, GitLab and Heroku. This write up is also helpful for someone who is just starting out. I have kept it as simple as possible to get things up and running.</p><h3>Uploading the project to GitLab</h3><ol><li>Create a Python virtual environment for us to use. Get into the virtual environment.</li><li>Create a sample Python Flask application on your machine.</li><li>Verify that everything is running fine.</li><li>Run the command pip freeze &gt; requirements.txt from within the main application folder to catch all the requirements for running your application.</li><li>Create a Procfile which is used by Heroku to declare what commands are run by your application on the Heroku platform. Procfile usually consists of the web server used to run the application. In our case let us use Gunicorn, the default Python WSGI HTTP server. The content of your Procfile will be web: gunicorn &lt;name of the app.py file&gt;:&lt;app-name&gt; where app-name is usually “app”. Place this file within the main application folder.</li><li>Now login (or signup) to GitLab and create a project. The moment you do this, you will get a standard set of instructions on how to “link” your code on your development machine to GitLab project. Just follow the commands, and after that you can do a git push or git pull to/from this project. This is a bit of an elaborate step and your last statement should look something like git push -u origin master. Once done, on refreshing the project page on GitLab, you should see all your code appear in GitLab.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pOEl0MEQvgDGE4hkryv7IQ.png" /><figcaption>Project repository in GitLab</figcaption></figure><h3>Linking GitLab and Heroku</h3><ol><li>Login to the <a href="https://www.heroku.com/">Heroku web portal</a> and create an application. Give it a nice name and runtime selection.</li><li>Now within the my_app folder on your development machine, create a file called “.gitlab-ci.yaml” (note the ‘.’ in the beginning).</li><li>This yaml file will have the following structure.</li></ol><pre>my_app_file_name:<br> script:<br> — apt-get update -qy<br> — apt-get install -y python-dev python-pip<br> — pip install -r requirements.txt<br> — export MONGOHQ_URL=$MONGO_URL<br> <br>production:<br> type: deploy<br> script:<br> — apt-get update -qy<br> — apt-get install -y ruby-dev<br> — gem install dpl<br> — dpl — provider=heroku — app=task-mgmt-app — api-key=$HEROKU_SECRET_KEY<br> only:<br> — master</pre><ol><li>Change my_app_file_name to the file name of your flask application. You need to set the HEROKU_SECRET_KEY variable in the project variables. You will get this key in the <a href="https://dashboard.heroku.com/">Heroku dashboard</a>. To set it in your GitLab project, go to <strong>Settings &gt; CI/CD Pipelines </strong>and search for <strong>Secret Variables. </strong>While using these variables in the yaml, we need to prepend the variable with the ‘$’ sign. It is a good practice not to share secret keys with anyone and also restrict access to them in the project.</li><li>You are almost there. Run the command git add .gitlab-ci.yml and git commit -m &lt;msg&gt; and git push -u origin master. You will see the file in the GitLab repository now.</li><li>On the GitLab “My Dashboard” page, click on <strong>Pipelines &gt; Jobs </strong>to see the job which has started running.</li><li>In case you are using a database in your App, you might want to link it to the App by placing the details in the .gitlab-ci.yaml file. Please check <a href="https://gitlab.com/bharathkallurs/tasks/blob/master/.gitlab-ci.yml">here</a> for an example. I have used MongDB in my application. Heroku provides adding a set of free libraries/apps to your application. There is an <a href="https://devcenter.heroku.com/articles/mongolab">mLab link</a> for adding MongoDB.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wWi-ip7uWAzV2g73xd8MRA.png" /><figcaption>Jobs list in the Pipeline</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T9MPnszPaFcXzJZYrqP7Vg.png" /><figcaption>A running job. Screenshot related to my_app_file_name in yaml.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l0yUXBu9bpphXcNSNUTrbQ.png" /><figcaption>Successful App deploy on Heroku</figcaption></figure><p>Yay! You have now successfully integrated your GitLab with Heroku with CI/CD configuration. Make all code changes you want in your repository, push it to the GitLab project, and see a job start every time there is a code push. For the current setup, I used GitLab public runners available <a href="http://gitlab.com/ci">here</a>. You can set up a custom GitLab runner and set appropriate configuration.</p><h3>Useful links :</h3><ol><li><a href="https://devcenter.heroku.com/articles/git#creating-a-heroku-remote">Creating heroku remote</a></li><li><a href="https://docs.gitlab.com/ce/ci/examples/test-and-deploy-python-application-to-heroku.html">Setting up CI/CD from GitLab on Heroku</a></li><li><a href="https://gitlab.com/bharathkallurs/tasks/tree/master">A task management application - repository : GitLab, deployed on Heroku</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e154db93952b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/free-code-camp/setting-up-a-ci-cd-on-gitlab-for-deploying-a-python-flask-application-on-heroku-e154db93952b">Setting up CI/CD on GitLab for deploying Python Flask application on Heroku</a> was originally published in <a href="https://medium.com/free-code-camp">We’ve moved to freeCodeCamp.org/news</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>