<?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: Suliman Awad</title>
    <description>The latest articles on DEV Community by Suliman Awad (@suliman99).</description>
    <link>https://dev.to/suliman99</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%2F1194735%2Fa0359f4b-3ef3-41ea-8436-600f2324bf4c.jpg</url>
      <title>DEV Community: Suliman Awad</title>
      <link>https://dev.to/suliman99</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suliman99"/>
    <language>en</language>
    <item>
      <title>The Best Way to Implement API Versioning in Django REST Framework (Agile) 💎</title>
      <dc:creator>Suliman Awad</dc:creator>
      <pubDate>Mon, 09 Sep 2024 11:09:06 +0000</pubDate>
      <link>https://dev.to/suliman99/the-best-way-to-implement-api-versioning-in-django-rest-framework-agile-492m</link>
      <guid>https://dev.to/suliman99/the-best-way-to-implement-api-versioning-in-django-rest-framework-agile-492m</guid>
      <description>&lt;p&gt;API versioning is one of the most important topics in backend development. It ensures that live applications continue running smoothly even when changes are made to the APIs, preventing any downtime or disruptions.&lt;/p&gt;

&lt;p&gt;In this article, we’ll explore how to implement API versioning in Django REST Framework in the best and simplest way.&lt;/p&gt;

&lt;h2&gt;
  
  
  API Versioning in the URL:
&lt;/h2&gt;

&lt;p&gt;Even if your project is already live and running on production servers, this method ensures backward compatibility. It allows for the version to be optional in the URL, defaulting to version 1 if no version is specified.&lt;/p&gt;

&lt;p&gt;To implement API versioning, we need to make three key changes to our project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a custom versioning class.&lt;/li&gt;
&lt;li&gt;Set the custom versioning class as the default in the project settings.&lt;/li&gt;
&lt;li&gt;Allow an optional version in the URL prefix for the entire project.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Creating the Versioning Class:
&lt;/h2&gt;

&lt;p&gt;First, create the following file in your project: &lt;code&gt;Project/common/rest_framework/api_versioning.py&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from rest_framework import versioning

class CustomURLPathVersioning(versioning.URLPathVersioning):
    default_version = 1
    version_param = 'version'

VERSION = r'v(?P&amp;lt;version&amp;gt;\d+)/'
OPTIONAL_VERSION = r'(v(?P&amp;lt;version&amp;gt;\d+)/)?'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Setting the Versioning Class as the Default:
&lt;/h2&gt;

&lt;p&gt;Next, set the versioning class as the default in your settings.py file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Project/settings.py

REST_FRAMEWORK = {
    # Other REST framework settings...

    'DEFAULT_VERSIONING_CLASS': 'common.rest_framework.api_versioning.CustomURLPathVersioning',
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accepting the Optional Version in the URL Prefix:
&lt;/h2&gt;

&lt;p&gt;Now, modify your urls.py file to allow an optional version in the URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Project/urls.py
from django.urls import path, re_path, include
from common.rest_framework.api_versioning import OPTIONAL_VERSION

app_patterns = [
    path('app1/', include('app1.urls')),
    path('app2/', include('app2.urls')),
]

urlpatterns = [
    # Other URL patterns...
    re_path(rf'^api/{OPTIONAL_VERSION}', include(app_patterns)),
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your project is already using path in the URL definitions, you’ll need to replace it with re_path and add the {OPTIONAL_VERSION} prefix to all your API URLs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage:
&lt;/h2&gt;

&lt;p&gt;Now that versioning is in place, you can access the version in your views using the request.version property. You can then handle your versioning logic based on this property. A common and clear approach is to return a specific serializer for each version.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class TestAPIView(APIView):
    def get_serializer_class(self):
        if self.request.version == 2:
            return TestSerializerV2
        return TestSerializer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the URL is called without a version (/api/endpoint or /api/v1/endpoint), the default version (1) will be used, returning the original serializer (TestSerializer).&lt;/li&gt;
&lt;li&gt;If the URL includes version 2 (/api/v2/endpoint), the version will be considered 2, and the new serializer (TestSerializerV2) will be returned.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;With this setup, you can easily implement API versioning in Django REST Framework without disrupting existing functionality or causing issues for live applications. This method ensures flexibility, stability, and smooth transitions as you evolve your API.&lt;/p&gt;

&lt;p&gt;Happy coding, and may your projects continue to run stably with proper API versioning! 😊&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>backend</category>
      <category>programming</category>
    </item>
    <item>
      <title>Automate Data Seeding in Django with django-seeding: A Developer's Delight 🔥</title>
      <dc:creator>Suliman Awad</dc:creator>
      <pubDate>Sat, 28 Oct 2023 22:27:31 +0000</pubDate>
      <link>https://dev.to/suliman99/automate-data-seeding-in-django-with-django-seeding-a-developers-delight-3oe</link>
      <guid>https://dev.to/suliman99/automate-data-seeding-in-django-with-django-seeding-a-developers-delight-3oe</guid>
      <description>&lt;p&gt;When building web applications, it's common to spend a significant amount of time 🧭 populating the database with needed data. Whether it's for development, testing, or demonstrations, having a streamlined way to fill your database can significantly boost productivity. That's where django-seeding comes into play ⭐.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Challenge of Data Seeding 🚨
&lt;/h2&gt;

&lt;p&gt;Picture this: You're developing a Django application, and you need to test how your system behaves with a large dataset. Manually creating thousands of records in the Django admin panel or through custom scripts can be tedious and time-consuming. What if there was a more efficient way to get this job done?&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing django-seeding 💎
&lt;/h2&gt;

&lt;p&gt;django-seeding is a Python package that empowers Django developers to automate the process of filling their databases with data. Whether you need data for testing, development, or simply to demonstrate your application, this package simplifies the task by introducing the ability to seed the data from CSV files or JSON files.&lt;br&gt;
The best part? It's incredibly easy to use.&lt;/p&gt;
&lt;h2&gt;
  
  
  Seeding Made Simple ✨
&lt;/h2&gt;

&lt;p&gt;with django-seeing, you can create data definitions in a matter of minutes. A data definition is a CSV file, JSON file, or a Python dictionary that represents your data. Here's a basic example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django_seeding import seeders
from django_seeding.seeder_registry import SeederRegistry
from django_seeding_example.models import M1

@SeederRegistry.register
class M1Seeder(seeders.CSVFileModelSeeder):
  model = M1
  csv_file_path = 'django_seeding_example/seeders_data/M1Seeder.csv'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we're seeding a M1 model with the data from the CSV file specified.&lt;/p&gt;

&lt;h2&gt;
  
  
  From CSVs to Data 👌
&lt;/h2&gt;

&lt;p&gt;django-seeding offers versatility. Not only can you define your data directly in your code, but you can also import data from CSV or JSON files. This means that you can prepare your data externally and then feed it into your Django application with ease.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boosting Developer Productivity 💰
&lt;/h2&gt;

&lt;p&gt;This package is a developer's delight. By automating data seeding, you can focus more on building features and less on tedious tasks. You'll find that development, testing, and debugging become smoother and more efficient.&lt;/p&gt;

&lt;h2&gt;
  
  
  Simplicity and Integration 😃
&lt;/h2&gt;

&lt;p&gt;django-seeding is designed to be straightforward to use. It integrates seamlessly with Django models and the Django ORM, making it a natural fit for Django projects. There's no need to learn a complex new system; it complements the Django environment you're already familiar with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enhance Your Workflow 🙌
&lt;/h2&gt;

&lt;p&gt;As a Django developer, you're constantly striving to enhance your workflow. django-seeding is a valuable tool for streamlining your development process. Whether you're testing a new feature or just need to demonstrate your app to a client, having a reliable way to populate your database can make a world of difference.&lt;br&gt;
Get Started with django-seeding&lt;br&gt;
Ready to give django-seeding a try? You can install it via pip:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install django-seeding&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;And then check out the comprehensive documentation to explore the various possibilities: &lt;a href="https://pypi.org/project/django-seeding/"&gt;https://pypi.org/project/django-seeding/&lt;/a&gt;&lt;br&gt;
Don't spend unnecessary time and effort manually creating test data for your Django application. Automate the process and see how django-seeding can elevate your development game.&lt;br&gt;
Happy seeding! 🎁&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>pypi</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
