<?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 Qeybulla Aliverdiyev on Medium]]></title>
        <description><![CDATA[Stories by Qeybulla Aliverdiyev on Medium]]></description>
        <link>https://medium.com/@qeybulla.aliverdev?source=rss-233b8961868d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vhn30TnZp-fgwe48472I8A.jpeg</url>
            <title>Stories by Qeybulla Aliverdiyev on Medium</title>
            <link>https://medium.com/@qeybulla.aliverdev?source=rss-233b8961868d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 18:30:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@qeybulla.aliverdev/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[Everything about Singleton Design Pattern]]></title>
            <link>https://blog.devgenius.io/everything-about-singleton-design-pattern-a685b6e168dc?source=rss-233b8961868d------2</link>
            <guid isPermaLink="false">https://medium.com/p/a685b6e168dc</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Qeybulla Aliverdiyev]]></dc:creator>
            <pubDate>Mon, 24 Apr 2023 08:16:52 GMT</pubDate>
            <atom:updated>2023-04-24T08:16:52.520Z</atom:updated>
            <content:encoded><![CDATA[<h4>Design Pattern Series: Singleton. It is easy to be single, but hard to remain</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*gzYBXk_beGMXurz0rt_OvA.png" /></figure><p>When writing a software project we may need an object to be single i. e. permit one and only one object of a particular class to exist. Thread pools, caches, dialog boxes, objects for logging must be single, in other case problems like overuse of resources, incorrect program behavior, etc. may occur.</p><p>Let’s have a class named <strong>Singleton</strong>. We will try to build this class in such a way that only one object of it will exist in the entire project.</p><p>An object is created with a constructor of the class. Having a public constructor means anyone can create a new object easily. So the first task is to make the constructor private:</p><pre>public class Singleton {<br>    // Prevents from creating new objects<br>    private Singleton(){  <br>    }<br>}</pre><p>But at least one object must be available, so how can we get that one if the constructor is private? The answer is, we create an object inside the class and a method to get that object:</p><pre>public class Singleton {<br>    private static Singleton singletonInstance;<br>    <br>    // Prevents from creating new objects<br>    private Singleton(){<br>    }<br>    <br>    public static Singleton getSingletonInstance(){<br>        return singletonInstance;<br>    }<br>}</pre><p>Did you notice the problem? If the object have not yet created <strong>singletonInstance </strong>will be null so the get method will return null. That’s why we should check this case inside the method, and if no instance has been created, we should create and pass to <strong>singletonInstance</strong>:</p><pre>public class Singleton {<br>    private static Singleton singletonInstance;<br>    // Prevents from creating new objects<br>    private Singleton(){<br>    }<br>    <br>    public static Singleton getSingletonInstance(){<br>    if (singletonInstance == null){<br>            singletonInstance = Singleton();<br>        }<br>        return singletonInstance;<br>    }<br>    <br>}</pre><p>In this implementation the instance is not created unless we need it. This is called <em>lazy instantiation</em>. It is one of the essential paradigms in software engineering, and crucial when making the project memory-efficient.</p><h3>Multithreading makes things trickier</h3><p>The last version of implementation looks complete but it’s not. What if we have multiple threads and each of them is accessing <strong>getSingletonInstance</strong> method at the same time when object isn’t created yet? In this case multiple new objects are created, which is what we want to avoid. The very first and simple solution that comes to the mind is making the get method synchronized. In that case multiple threads cannot access the method simultaneously.</p><pre>public static synchronized Singleton getSingletonInstance(){<br>    if (singletonInstance == null){<br>        return new Singleton();<br>    }<br>    return singletonInstance;<br>}</pre><p>Synchronization should be avoided when there is no need for it. In this case, we only need synchronization in the very first time when object is created. After that, even if multiple threads call the method at the same time, the single object will be returned so we do not need synchronization anymore. So we have to rethink about the implementation. There is multiple solutions depending on the situation:</p><ul><li>If the object of the class will not be needed too often, we can keep the last version. However, if we take into account that synchronization can reduce efficiency significantly, and we will need the frequently we take an action</li><li>Switching to eager instantiation: We can create an object and initialize the static variable in the beginning so there will always be an object:</li></ul><pre>public class Singleton {<br>    private static Singleton singletonInstance = new Singleton();<br>    // Prevents from creating new objects<br>    <br>    private Singleton(){<br>    }<br>    <br>    public static synchronized Singleton getSingletonInstance(){<br>        return singletonInstance;<br>    }<br>}</pre><p>This implementation is guaranteed to be thread-safe. Drawback is that we have an instance created in the beginning and if we do not need it this is not memory-efficient.</p><ul><li>Double-checked locking method: We can make the get method synchronized when there is no object created.</li></ul><pre>public class Singleton {<br>    private volatile static Singleton singletonInstance;<br>// Prevents from creating new objects<br>    private Singleton(){<br>    }<br>    public static Singleton getSingletonInstance(){<br>        if (singletonInstance == null){<br>            synchronized (Singleton.class){<br>               if (singletonInstance == null){<br>                    singletonInstance = new Singleton();<br>               }<br>            }<br>        }<br>        return singletonInstance;<br>    }<br>}</pre><p>Volatile keyword ensures multiple threads to work with the instance correctly and access to it from main menu, not from the cache. <a href="https://www.javatpoint.com/volatile-keyword-in-java">More about volatile</a>.</p><p>In this code the synchronized block is only accessed when there is no object beforehand. Starting from the second time this block will not be executed and the object will be returned directly. Inside the synchronized block once more the variable is checked and it’s still null, a new instance is created.</p><p><em>This method does not work in Java 1.4 and earlier.</em></p><p>To sum up, Singleton Design Pattern is one of the mostly used design patterns. It is the simplest one according to the problem it solves and implementation, however, one should be careful when using Singleton pattern due to those multithreading issues.</p><p>Hope you found it useful.</p><p>You can contact me <a href="https://www.linkedin.com/in/qeybulla-aliverdiyev-72443a193/">here</a>.</p><p>Thanks for reading! Follow for more contents about Design Patterns and other Software Engineering Concepts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a685b6e168dc" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/everything-about-singleton-design-pattern-a685b6e168dc">Everything about Singleton Design Pattern</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to store text with some styles in Django model]]></title>
            <link>https://awstip.com/how-to-store-text-with-some-styles-in-django-model-26aa98c3e232?source=rss-233b8961868d------2</link>
            <guid isPermaLink="false">https://medium.com/p/26aa98c3e232</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[django-admin]]></category>
            <dc:creator><![CDATA[Qeybulla Aliverdiyev]]></dc:creator>
            <pubDate>Thu, 04 Aug 2022 15:14:25 GMT</pubDate>
            <atom:updated>2022-08-15T12:43:26.568Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Django Rich text" src="https://cdn-images-1.medium.com/max/491/1*eStcyXlOk_LxNlFLd9eZAA.png" /></figure><p>If we want to store a column which contains values in text format in a database table we will, of course, use TextField which comes from django.db.models . In this case, we will be storing only a raw text without any styles. If we look at the admin page we will see the prove:</p><figure><img alt="TextField in admin panel" src="https://cdn-images-1.medium.com/max/784/1*ZEB3RvNi26XzW4bpP8VYbA.png" /><figcaption>TextField in admin panel</figcaption></figure><p>In order to store text with some styles we will use a package called “django-richtextfield”.</p><h4>Installation</h4><p>Install the package using pip:</p><pre>$ pip install django-richtextfield</pre><p>Update your INSTALLED_APPS section in the settings.py</p><pre>INSTALLED_APPS = [<br>    &#39;django.contrib.admin&#39;,<br>    ...<br>    &#39;djrichtextfield&#39; # add this<br>]</pre><p>Then update your project-level urls file:</p><pre>path(&#39;djrichtextfield/&#39;, include(&#39;djrichtextfield.urls&#39;))</pre><p>In the settings file, configure some default setting for the text:</p><pre>DJRICHTEXTFIELD_CONFIG = {<br>    &#39;js&#39;: [&#39;//cdn.tiny.cloud/1/no-api-key/tinymc/5/tinymce.min.js&#39;],<br>    &#39;init_template&#39;: &#39;djrichtextfield/init/tinymce.js&#39;,<br>    &#39;settings&#39;: {<br>        &#39;menubar&#39;: False,<br>        &#39;plugins&#39;: &#39;link image&#39;,<br>        &#39;toolbar&#39;: &#39;bold italic | link image | removeformat&#39;,<br>        &#39;width&#39;: 700<br>    }<br>}</pre><p>Now, lets add RichTextField to a model:</p><pre>from djrichtextfield.models import RichTextField<br>from django.db import models</pre><pre>class Mail(models.Model):<br>    ...<br>    text = RichTextField()<br>    ...</pre><p>Here we go! We can see the new format in the admin page: (do not forget to run migrations)</p><figure><img alt="RichTextField in admin panel" src="https://cdn-images-1.medium.com/max/808/1*WzaFwi1NC3C-Rm8R_Noaeg.png" /><figcaption>RichTextField in admin panel</figcaption></figure><p>Hope you found it useful.</p><p>You can contact me <a href="https://www.linkedin.com/in/qeybulla-aliverdiyev-72443a193/">here</a>:</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26aa98c3e232" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/how-to-store-text-with-some-styles-in-django-model-26aa98c3e232">How to store text with some styles in Django model</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Complete guide to translation Django model fields]]></title>
            <link>https://blog.devgenius.io/complete-guide-to-translation-django-model-fields-ed926a463689?source=rss-233b8961868d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed926a463689</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[django-framework]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <dc:creator><![CDATA[Qeybulla Aliverdiyev]]></dc:creator>
            <pubDate>Wed, 16 Mar 2022 18:04:26 GMT</pubDate>
            <atom:updated>2022-03-18T22:20:09.660Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to make model fields translatable in Django</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*9YdpI5RsfQWWitqIwZJ9JA.png" /></figure><p>Having the content in multiple languages is one of the key features of modern websites. In this article we are going to see how to make Django models fields translatable.</p><p>We will use <em>django-modeltranslation</em> package. You can learn more about it <a href="https://django-modeltranslation.readthedocs.io/en/latest/">here</a>.</p><h3>Models</h3><p>Let’s assume that we have a Person model with three fields: name, surname and profession.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff5758897395c580331b22c6e103f6f6/href">https://medium.com/media/ff5758897395c580331b22c6e103f6f6/href</a></iframe><p>After registering the model in the admin.py file we can add Person objects through admin panel. We will have a simple view, serializer and url to see the data as json response in django rest framework. For the sake of simplicity I will not show creating all this stuff because the main focus is making the fields translatable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ap-_8Cp-nn9kawq3F4ZFNw.png" /><figcaption>Adding person object in admin interface</figcaption></figure><p>We can see from picture that we can add the fields in only one language (English or any other).</p><p>Let’s see the data as json response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FA76fM4ElmcWaBACDfYPVQ.png" /></figure><h3>Setup</h3><p>Let’s now install the package mentioned above:</p><pre>$ pip install django-modeltranslation</pre><p>After installation succeeds, we have to add modeltranslation to the INSTALLED_APPS in settings.py of the project.</p><p>In settings file we have to set USE_I18N = True</p><p>Then we should set the LANGUAGES</p><pre>gettext = lambda s: s<br>LANGUAGES = (<br>    (&#39;en&#39;, gettext(&#39;English&#39;)),<br>    (&#39;ru&#39;, gettext(&#39;Russian&#39;)),<br>)</pre><p>Now, we should create translation.py in the app in which we are storing the models.py file. In this file we should register TranslationOptions for the models in question.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5a02bd03d438191d3b1cf7f49e6fa1a/href">https://medium.com/media/a5a02bd03d438191d3b1cf7f49e6fa1a/href</a></iframe><p>fields contains the field names that we want to be translatable.</p><p>We should run migrations and migrate them to the database:</p><pre>$ python manage.py makemigrations<br>$ python manage.py migrate</pre><p>However, if we want to add a new Person object in admin panel, we will see that there are still one box for each field which means we can add the value in one language only.</p><p>That’s why we have to change the admin.py. PersonAminclass must inherit from TranslatioAdminfrom modeltranslation.admin. PersonAdminclass will not contain anything since we don’t customize the admin interface in this stage.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb35d311c4623cecc6c61e4077537f04/href">https://medium.com/media/eb35d311c4623cecc6c61e4077537f04/href</a></iframe><p>New fields can be seen in admin panel when tying to add new Person or changing the existing one:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*BahNp785Qnss4Cb3D_s-3A.png" /><figcaption>New fields in admin</figcaption></figure><p>Let’s add two objects again with new fields and check the json respone:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NhRdNEpnKqU1k_HK98EAQw.png" /></figure><p>We can see that two fields have been added, for example name_ru and name_en. If we try to get the name of the object, we will use object.name and it will give us the value in default language. By defalut, the default language is the one which is spesified in settings.py by LANGUAGE_CODE.</p><h3>Handling default language in request header</h3><p>Because we spesify the default language in projects settings, for all users in all requests we get the response in the same language specified in settings file. You would agree that it is not what we want because we want each user get the response in its specific language. One of the ways to handle it is to send the language in the request header. We will send Accept-Language header in request. Let’s change the view to get the language from the request header and activate it so we get the default value in this language.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b63bb25fccd4df710390711bdab4ad09/href">https://medium.com/media/b63bb25fccd4df710390711bdab4ad09/href</a></iframe><p>We have added get_queryset function in which we will get the language from the request header and activated it using translation.activate from django.utils</p><p>Firstly the existence of HTTP_ACCEPT_LANGUAGE in the request headers should be checked (the if statement). If it does exist there, we get its value from the dictionary containing the meta data on the request, such as headers. After that we activate this language for response using translation.activate.</p><p>For testing I will use <a href="https://install.advancedrestclient.com/install">Advanced Rest Client</a>, but you can use any other client you want, such as <a href="https://www.postman.com/">Postman</a>.</p><p>We add a header in ARC, choosing Accept-Language in Name section and en-US (for English) or ru (for Russian) in Value section.</p><p>Let’s first choose en-US and see the response data:</p><figure><img alt="ARC request for english" src="https://cdn-images-1.medium.com/max/995/1*eu6qbhYJaEPu243J0z1i2w.png" /></figure><figure><img alt="Json response for english" src="https://cdn-images-1.medium.com/max/962/1*lqOddmJpqG_anRJncZNzLg.png" /></figure><p>Now lets choose ru and see if the default fields are in Russian language:</p><figure><img alt="request headers for russian" src="https://cdn-images-1.medium.com/max/986/1*YyUcfFQTp9XT1n8hcL8BTw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*Xw35xR4zivtaB1qlvV6xLQ.png" /></figure><p>It can be clearly seen that default fields contains the values in the language that we specified in the request headers.</p><h4>Additional note</h4><p>We can have a special default language field in user model so if the user is logged in we can get the language from the user table directly, not from the request headers. This helps to have the same language for same user in different devices.</p><h4>References</h4><p><a href="https://django-modeltranslation.readthedocs.io/en/latest/index.html">https://django-modeltranslation.readthedocs.io/en/latest/index.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed926a463689" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/complete-guide-to-translation-django-model-fields-ed926a463689">Complete guide to translation Django model fields</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to combine two querysets in one endpoint in Django Rest Framework]]></title>
            <link>https://medium.com/@qeybulla.aliverdev/how-to-combine-two-querysets-in-one-endpoint-in-django-rest-framework-f7e8c151bfc3?source=rss-233b8961868d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7e8c151bfc3</guid>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Qeybulla Aliverdiyev]]></dc:creator>
            <pubDate>Thu, 10 Mar 2022 18:44:36 GMT</pubDate>
            <atom:updated>2022-03-10T18:52:27.452Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/730/0*lpnT1ZXKyJHk5hwT.png" /></figure><p>Returning a data from a model in Django Rest Framework is straightforward. We will need a urlpattern, a view and a serializer for this purpose. But sometimes, when writing a REST API in DRF, we may want our endpoint to return the data from two or more database tables (models). Recently in one of my projects I had this task and now I will share how I achieved to manage it.</p><p>Let’s assume that we have two models. <strong>Project </strong>and <strong>News</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81dcc32e0ff2b0aaf8ccf857a1bf9534/href">https://medium.com/media/81dcc32e0ff2b0aaf8ccf857a1bf9534/href</a></iframe><p>Now, of course after running migrations and migrating the changes to database we can create serializers for each model:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1872ef4d88cc02d51420a0bf1e918f8/href">https://medium.com/media/e1872ef4d88cc02d51420a0bf1e918f8/href</a></iframe><p>After creating views and urls we can see data from each model separately in DRF Browsable API:</p><figure><img alt="A list of Projects" src="https://cdn-images-1.medium.com/max/1024/1*kL_GgeTiYVLrqAfz2hZNuw.png" /><figcaption>Projects List</figcaption></figure><figure><img alt="A list of News" src="https://cdn-images-1.medium.com/max/1024/1*AjaJVE8XPJ8V94W7FdNRtQ.png" /><figcaption>News list</figcaption></figure><p>Now we want to have one endpoint that returns two querysets at once. We will create a new function-based view that is responsible for running the logic.</p><p>Firstly we should get the querysets.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9eb1a347a9560b06c79008ae6af45788/href">https://medium.com/media/9eb1a347a9560b06c79008ae6af45788/href</a></iframe><p>Then we should serialize the data in the querysets.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3c8c3633c56f0acbb39591e6c03e68c/href">https://medium.com/media/a3c8c3633c56f0acbb39591e6c03e68c/href</a></iframe><p>These objects contains serialized data so we can access the data by “<em>object.data</em>”.</p><p><em>projects_serializer.data</em> will give us a list containing the data from the database as <a href="https://www.geeksforgeeks.org/ordereddict-in-python/">OrderedDict</a>. Same is true for <em>news_serializer</em>, too.</p><p>We can add these two lists and return.</p><p>We cannot return the data directly, it should be returned as either <em>HttpResponse </em>or <em>Response</em>. In DRF we use <em>Response </em>from <em>rest_framework.response</em>.</p><p>Our full view function will be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14c25b375f090cfa8270116afb76d2e6/href">https://medium.com/media/14c25b375f090cfa8270116afb76d2e6/href</a></iframe><p>We can see two data had been returned in one request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*49JtHXH2laUv2HrD4hdF0A.png" /></figure><p>So we achieved to get data from two different database tables with one request/response.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7e8c151bfc3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>