Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Fixing Up Permissions After Data Migration in Django
Hello djangonauts, today I wanna talk about a problem that I faced while working with Django. So here’s the thing I just recently performed a major release at work, the database had to be changed since we had newer migrations and database structure. Naturally I had to import some data from my older database to the new one, which for most of the part could safely be done using fixtures (or so I thought). So I would like to share some tips and things that I learned recently because of it. Tip #1 Always exclude the Content Type table while exporting a fixture for data migration. It can be done as follows: python manage.py dumpdata --exclude=contenttypes Why? You may ask? It’s because Django automatically creates these records when you perform the migrations for your apps. These records are basically used by django internally for schema management. So if you import them from the older database, Django would raise errors. Tip #2 Now if you were like me, you might have dumped your permissions records as well into the fixture, that’s where the fun part comes in. Now since content type was created afresh for the newer database, so their ids … -
August 2020 Courses
Live, Interactive, Online Courses For years Audrey and I have travelled giving in-person training sessions. With current events being what they are, I've switched to giving live, online, interactive trainings using Zoom meeting software. This has involved some major refactoring of content. Here are the upcoming courses on my calendar: August 14: Django Best Practices the Two Scoops Way August 21, 22, 23: Django Crash Course Live -
August 2020 Courses
Live, Interactive, Online Courses For years Audrey and I have travelled giving in-person training sessions. With current events being what they are, I've switched to giving live, online, interactive trainings using Zoom meeting software. This has involved some major refactoring of content. Here are the upcoming courses on my calendar: August 14: Django Best Practices the Two Scoops Way August 21, 22, 23: Django Crash Course Live -
Distributed Locking in Django
As you start scaling an application out horizontally (adding more servers/instances), you may run into a problem that requires distributed locking. That's a fancy term, but the concept is simple. Sometimes you have to be sure that when a block of code is running (usually modifying data somewhere), no other instances runs that same block of code. Ideally, you can design your code not to require locks, but sometimes it is inevitable. In general, locks are used when you need to modify state (e.g. the database) in an atomic manner. Some examples of when you might need a distributed lock: Cron jobs/scheduled tasks whose runtime may exceed the interval with which they are triggered Flushing a write-back cache to the database. Bulk processing files If running the code on multiple servers simultaneously would result in corrupted or duplicate data, you probably need to use a distributed lock. The code in question will acquire the lock before execution. Once it has the lock, any other attempt to acquire it will fail. Implementations Lock data must be stored in a location accessible to all application instances. If this is a standard Django site, you probably already have two such systems available to … -
Distributed Locking in Django
As you start scaling an application out horizontally (adding more servers/instances), you may run into a problem that requires distributed locking. That's a fancy term, but the concept is simple. Sometimes you have to be sure that when a block of code is running (usually modifying data somewhere), no other instances runs that same block of code. Ideally, you can design your code not to require locks, but sometimes it is inevitable. In general, locks are used when you need to modify state (e.g. the database) in an atomic manner. Some examples of when you might need a distributed lock: Cron jobs/scheduled tasks whose runtime may exceed the interval with which they are triggered Flushing a write-back cache to the database. Bulk processing files If running the code on multiple servers simultaneously would result in corrupted or duplicate data, you probably need to use a distributed lock. The code in question will acquire the lock before execution. Once it has the lock, any other attempt to acquire it will fail. Implementations Lock data must be stored in a location accessible to all application instances. If this is a standard Django site, you probably already have two such systems available to … -
Fixing Up Permissions After Data Migration in Django
Find out how I fixed my permissions after I imported my data -
The best frontend JavaScript framework for Django
A question I've seen asked a lot is "what's the best frontend JavaScript framework to use with Django". Django itself doesn't make any recommendation on which frontend framework to use, or even assumes you're using a frontend framework at all. So, which frontend framework should you be using? And which one "plays well" with Django? Defining "the best" The problem with such questions is that "the best" is often ill-defined. Best based on what criteria? If you're starting a new project and wondering which one to choose, the disappointing answer I have for you is this: The best frontend JavaScript framework is one you already know well That's it. If you, or your team, wants to use a frontend framework, the best one to use is the one that you and your team have the most familiarity with. That's because it's very likely that you will be more productive with it. Django "plays well" with any frontend JavaScript framework, by virtue of the fact that it makes no assumptions and does not force you to use any framework. In that respect, it's all the same. At the end it's all JavaScript. The actual question But of course, that's not what … -
Django tarpit for malicious requests
While checking my logs for this site and seeing the usual malicious requests against every spammers favorite software WordPress I decided to make things a little harder for them with a [tarpit](https://en.wikipedia.org/wiki/Tarpit_(networking)). -
Thoughts on „Surviving Django“
Daniele, the author of psycopg2, wrote an interesting post about Django and its migration system. Having used Django for well over a decade and having worked on projects either handed to me or greenfield at different scales I cannot agree with his arguments as they are stated. The migration and ORM system does solve a far larger problem than allowing web apps to switch between databases. Djangos migration system is primarily a developer tool. A rather excellent one I cannot praise enough. It abstracts and standardises the way database migrations are handled. Remember following a document outlining the order of 15 migrations to apply when doing a deploy? Remember trying to add a 16th? I surely prefer running makemigrations. Is it perfect? No. Can a talented DBA get far more out of a database? Absolutely. Is any of that more relevant than safeguards and ease of use for 90% of the apps out there? Definitely not. Now to the really good part – you can have the best of both worlds! Migrations can run Python and SQL, while still giving you some of the safeguards and making it comfortable to use. They will not get in your way, but spare … -
Thoughts on „Surviving Django“
Daniele, the author of psycopg2, wrote an interesting post about Django and its migration system. Having used Django for well over a decade and having worked on projects either handed to me or greenfield at different scales I cannot agree with his arguments as they are stated. The migration and ORM system does solve a far larger problem than allowing web apps to switch between databases. Djangos migration system is primarily a developer tool. A rather excellent one I cannot praise enough. It abstracts and standardises the way database migrations are handled. Remember following a document outlining the order of 15 migrations to apply when doing a deploy? Remember trying to add a 16th? I surely prefer running makemigrations. Is it perfect? No. Can a talented DBA get far more out of a database? Absolutely. Is any of that more relevant than safeguards and ease of use for 90% of the apps out there? Definitely not. Now to the really good part - you can have the best of both worlds! Migrations can run Python and SQL, while still giving you some of the safeguards and making it comfortable to use. They will not get in your way, but spare … -
Django News - Django 3.1 Release - Aug 7th 2020
News Django 3.1 Released 3.1 is live! You can read the release notes. Please take a moment to thank our two Django Fellows, @MariuszFelisiak and @carltongibson, who made this release possible. djangoproject.com Django REST Framework 3.11.1 Released github.com Upgrade to pip 20.2, plus, changes coming in 20.3 The newest version of pip, 20.2, is now released. You can install it by running python -m pip install --upgrade pip. blogspot.com Django bugfix releases issued: 3.0.9 and 2.2.15 djangoproject.com Python Developers Survey 2019 Results The results of JetBrains' annual survey is out. jetbrains.com Articles Django 3.1 Async Django 3.1 introduces asynchronous views, middlewares, and tests. This article walks you through those features. wersdoerfer.de A breakdown of how NGINX is configured with Django Matt Segal covers Nginx in depth. mattsegal.dev Backporting a Django ORM Feature with Database Instrumentation How to use database instrumentation to backport a Django ORM feature. adamj.eu Some SQL Tricks of an Application DBA Haki Benita's guide to non-trivial tips for database development. hakibenita.com Goodconf: A Python Configuration Library Lincoln Loop recently released Goodconf and this is their guide to why and how to use it. lincolnloop.com Why you shouldn’t remove your package from PyPI Gonçalo Valério covers how to … -
How to make Django request.is_ajax() work with JS fetch()
Django's request object has a nifty little method is_ajax. It allows determining whether a request comes from a JS framework (aka old-school ajax). While it works fine with some JS libraries, including the venerable jQuery, it won't work with modern JS native fetch API out of the box. … Read now -
Introducing Django ClearCache 🤠🧹💰 - allows to clear cache via admin UI or command line
I've been working with the Django cache recently. And while Django has exceptional caching capabilities, I was surprised to find out that it doesn't provide a simple way to manually clear a cache. I checked online and found a couple of clear cache packages for Django, but all of them … Read now -
Brief reflections on programming languages I have worked with during my career
I've played and worked with multiple programming languages over my 15-year long career in development (and a bit before, when I studied at school and university). Every language I touched left a piece of knowledge or a feeling in me. So I decided to remember them all and try to … Read now -
The Simplest Way to Create a Python Virtual Environment
A lot of fog and mysteries surround Python virtual environments. There are loads of packages, tools, and wrappers trying to make developer's life with virtual environments easier. But if we look at the basics, we'll see that actually, creating a virtual environment in Python has never been easier. Starting from … Read now -
How to sort Django admin list column by a value from a related model
Here's the real-life case. On this blog, I have two models: Post and PostStats. The former is, as you might have guessed, for posts, while the latter is for per post statistics like a view count. Here's a simplified version of both models: class Post(models.Model … Read now -
Non-Django issues during a Django upgrade
Upgrading from Django 3.0.9 to 3.1 was a breeze. To be fair upgrading Django is usually a breeze. The exhausive release notes written every time a new version is rolled out is a defining factor here. What do I mean when I say “a breeze”? I compare Django “upgrades” to something like upgrading a Python 2 codebase to Python 3 😬 So, upgrades being a breeze, why write a post about this topic? Because a Django application usually does not comprise only of the Django package. How I upgraded This section basically is about “how I usually upgrade”. Made specific for this 3.0.9 to 3.1 upgrade. Steps Changed Django version in requirements.txt file. I.e. changed entry from Django==3.0.9 to Django==3.1. Ran pip install -r requirements.txt with the updated requirements file. Among the command’s output I spotted: Installing collected packages: Django Found existing installation: Django 3.0.9 Uninstalling Django-3.0.9: Successfully uninstalled Django-3.0.9 Successfully installed Django-3.1 Ran ./manage.py check This yielded several warnings of the type: $ ./manage.py check System check identified some issues: WARNINGS: app_name.Model.field_name: (fields.W904) django.contrib.postgres.fields.JSONField is deprecated. Support for it (except in historical migrations) will be removed in Django 4.0. HINT: Use django.db.models.JSONField instead. ... System check identified 3 issues … -
Thoughts on „Surviving Django“
Thoughts on „Surviving Django“ Daniele, the author of psycopg2, wrote an interesting post about Django and its migration system. Having used Django for well over a decade and having worked on projects either handed to me or greenfield at different scales I cannot agree with his arguments as they are stated. The migration and ORM system does solve a far larger problem than allowing web apps to switch between databases. Djangos migration system is primarily a developer tool. A rather excellent one I cannot praise enough. It abstracts and standardises the way database migrations are handled. Remember following a document outlining the order of 15 migrations to apply when doing a deploy? Remember trying to add a 16th? I surely prefer running makemigrations. Is it perfect? No. Can a talented DBA get far more out of a database? Absolutely. Is any of that more relevant than safeguards and ease of use for 90% of the apps out there? Definitely not. Now to the really good part - you can have the best of both worlds! Migrations can run Python and SQL, while still giving you some of the safeguards and making it comfortable to use. They will not get in … -
Give Me A Break... Day - Building SaaS #67
In this episode, we did some Django model development. I created a new model to track break days in a school year. This model will be critical to fill in vacations and holidays so that the scheduling functionality works properly. I added the model, the tests, the admin page, and the create view to create break days in the app. We started by picking a model name and discussing naming in programming. -
Multiple Image Files Upload with Django 3, Angular 10 and FormData
In the previous tutorial we have seen how to implement image file uploading in Django 3 and Angular 10. In this tutorial, we'll see how to implement multiple file uploading with FormData and HttpClient. Note: It's recommended that you start from the previous tutorial to see detailed steps of how to create a django 3 project, how to install Angular 10 CLI and generate a new Angular 10 project along with services and components as we won't cover these things in this tutorial. Working with Angular 10, Django 3 and FormData In this tutorial, we'll see how to use HTML5 FormData with Angular and django 3 to upload multiple image files from an Angular 10 frontend to a Python server. Let's get started by cloning a django 3 project with the single file uploading that was already implemented. Cloning the Angular 10 Upload Frontend If you don't want to follow the steps from the previous part, you first need to get the project we've built. Open a new terminal and run the following command: $ git clone https://github.com/techiediaries/django-angular-file-upload-example.git Next, navigate inside the project's folder and install the npm dependencies using the following command: $ cd django-angular-file-upload-example $ npm install Next, … -
WARNINGS (URLs.W005) URL namespace ‘admin’ isn’t unique. You may not be able to reverse all URLs in this namespace
Lets jump into this minor issue you face while running Django migrations. The issue also has a quick fix. The issue is encountered when we type the following command in your terminal: -
Django Password Reset Tutorial
In this tutorial we'll add a password reset sequence to our Django application. This builds upon our previous work where we added [Login & Logout](https://learndjango.com/tutorials/django-login-and-logout-tutorial) pages and then a [Signup … -
How I got a residency appointment thanks to Python, Selenium and Telegram
Hello everyone As some of you might know, I’m a Venezuelan 🇻🇪 living in Montevideo, Uruguay 🇺🇾. I’ve been living here for almost a year, but because of the pandemic my residency appointments have slowed down to a crawl, and in the middle of the quarantine they added a new appointment system. Before, there were no appointments, you just had to get there early and wait for the secretary to review your files and assign someone to attend you. But now, they had implemented an appointment system that you could do from the comfort of your own home/office. There was just one issue: there were never appointments available. That was a little stressful. I was developing a small tick by checking the site multiple times a day, with no luck. But then, I decided I wanted to do a bot that checks the site for me, that way I could just forget about it and let the computers do it for me. Tech Selenium I had some experience with Selenium in the past because I had to run automated tests on an Android application, but I had never used it for the web. I knew it supported Firefox and had … -
How I got a residency appointment thanks to Python, Selenium and Telegram
Hello everyone As some of you might know, I’m a Venezuelan 🇻🇪 living in Montevideo, Uruguay 🇺🇾. I’ve been living here for almost a year, but because of the pandemic my residency appointments have slowed down to a crawl, and in the middle of the quarantine they added a new appointment system. Before, there were no appointments, you just had to get there early and wait for the secretary to review your files and assign someone to attend you. But now, they had implemented an appointment system that you could do from the comfort of your own home/office. There was just one issue: there were never appointments available. That was a little stressful. I was developing a small tick by checking the site multiple times a day, with no luck. But then, I decided I wanted to do a bot that checks the site for me, that way I could just forget about it and let the computers do it for me. Tech Selenium I had some experience with Selenium in the past because I had to run automated tests on an Android application, but I had never used it for the web. I knew it supported Firefox and had … -
A minimal Websockets setup with Django in production
That's me wiring up my cool JS grid to autoload data changes via server side push. Yeah! Image credit: thedisgruntledsherpaproject.com Use Case I have a Handsontable implementation for an underlying database table. I.e. a “JavaScript data grid that looks and feels like a spreadsheet”. A requirement came up. Obvious in hindsight. Changes made to cells in one sheet should be reflected in the same sheet for other users. When the sheet is open in other browser tabs/windows. This calls for “server side push” using web sockets. I.e. the server needs to push a notification to open “clients”. Another way to do this would be to have client browsers Ajax-polling for changes. But that would be wasteful. Let’s only update the sheet when valid changes are saved! What does minimal mean in this case? This application is used by a team internally. Usage not exceeding ten concurrent users. The implications of this: One process to serve web socket requests is enough. No real performance testing was done. No consideration of alternatives to daphne such as uvicorn or starlette. I picked up daphne because it came up “first on the list of alternatives”. That’s it! No need to handle websocket interactions asynchronously …