Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Optimize Django memory usage
Django memory usage is usually quote good, but sometimes – if you use the Django ORM without really knowing what is doing behind the scenes – you can see a huge spike in RAM usage. Fortunately there exist some simple methods to optimize Django memory usage. Table of Contents 1. The problem 2. A bit of Django memory profiling 3. Optimize Django memory usage: using iterator() method 4. Optimize Django memory usage: using pagination 5. Conclusion 1. The problem Consider this is the apparently innocent view from django.http import HttpResponse from .models import FirstModel # this view will make crazy use of the RAM ;) def my_view(request): # this queryset contains about 100k records # each of them has many ForeignKeys to other models huge_queryset = FirstModel.objects.all() f = open('dumb.dump', 'w') for record in huge_queryset: print >>f, record f.close() return HttpResponse('Dumb dump completed!') as you can see it’s very simple, the peculiarity here is the dimension of the queryset, because the table contains about 100k records, and each record has several ForeignKey fields to other models. An experienced Django developer should immediately see the problem, but it can be interesting to analyze the problem a little bit more. If you … -
Django News - Issue 16 - Mar 27th 2020
News Wagtail roadmap priorities 2020 We're reviewing our roadmap for the next 12 months and we'd love your input. If you use Wagtail, please help shape our plans. google.com PyCon US 2020 in Pittsburgh, Pennsylvania is cancelled The PSF’s priority is the health and safety of the community and the COVID-19 (Coronavirus) pandemic has made it unsafe to hold PyCon US this year. blogspot.com Pipenv new release A new Pipenv release is due at the end of the month. google.com Articles Using Django Check Constraints to Ensure Only One Field Is Set From Adam Johnson, a demonstration of using check constraints in a Django model. adamj.eu How Eldarion Works Remotely — Eldarion Blog Notes on working remotely from a leading Django consultancy. eldarion.com East Meets West When Translating Django Apps Notes from a PyCascades 2020 talk on using Django's built-in translation app. automationpanda.com Postgres VIEW from Django QuerySet How (and why) to use Django's ORM to generate a Postgres VIEW. schinckel.net How to restrict access with Django Permissions A look at the various ways to implement Django permissions. coderbook.com Double-checked locking with Django ORM How to implement double-checked locking within Django. lukeplant.me.uk Designing a User-Friendly ML Platform with Django Creating … -
Starting the Onboarding Flow - Building SaaS #49
In this episode, we worked on the progress element that will display in every step of the onboarding flow. I added some labels and styled the banner using Tailwind CSS. At the end of the stream, we boxed in the shape of the welcome page with some placeholder elements. The very first thing I did was insert a top bar that was unstyled to the top of the welcome page. We added some placeholder text for each of the steps in the onboarding flow. -
Google Summer of Code - Sage Abdullah
Weekly Django Chat NewsletterGoogle Summer of Code 2020 (March 31st deadline)Sage’s GSOC proposalSage’s GSOC Blogmwparserfromhell - A Python parser for MediaWiki wikicode -
Using Django Check Constraints to Ensure Only One Field Is Set
I previously covered using Django’s CheckConstraint class to validate fields with choices and percentage fields that total 100%. Here’s another use case. A project I was working on recently stores “scores” which may have a single typed value: an integer, or a decimal, or a duration. Because they have different types, they are stored in separate columns in the database. Therefore, for a single score, only one of these columns may be filled in (not null). You could solve this by using model inheritance. The downside of this is that using either concrete or abstract inheritance, you’d need multiple tables. This is also a lot of work for a single differing field. Instead of this, we settled on a single model appraoch with multiple value columns, only one of which should be set. The model looks something like this: from django.db import models class ScoreType(models.IntegerChoices): POINTS = 1, 'Points' DURATION = 2, 'Duration' class Score(models.Model): type = models.IntegerField(choices=ScoreType.choices) value_points = models.IntegerField() value_duration = models.DurationField() (IntegerChoices is one of Django 3.0’s new enumeration types.) If type is ScoreType.POINTS, the value_points column should be set. And likewise, if the type is ScoreType.DURATION, the value_duration column should be set. However confident you are … -
More on service layers in Django
Well, that provoked some discussion. While there were plenty of people who agreed with the general idea of that post, there were also quite a few objections. And most of those seem to fall into two main categories: people who want some type of additional layer (and may or may not call it a “service”) as a way of managing cross-cutting complexity, and people who want it as an isolating abstraction for testing. There’s also a third … Read full entry -
Setting Python's Decimal Context for All Threads
Python’s decimal module has concept of a “context.” This defines the default precision of new Decimals, how rounding works, and lots of other behaviour. Maths gets complicated! It’s quite common to need to customize the decimal context, to control one of these features. As the decimal documentation states, contexts are per-thread: The getcontext() function accesses a different Context object for each thread. Having separate thread contexts means that threads may make changes (such as getcontext().prec=10) without interfering with other threads. This isolation is good, but it also means that it’s an easily forgotten concern when adding threading. This could cause hard-to-discover arithmetic bugs. Oh no! Thankfully, there’s a fix, as per the decimal documentation: [For new threads] the new context is copied from a prototype context called DefaultContext. To control the defaults so that each thread will use the same values throughout the application, directly modify the DefaultContext object. This should be done before any threads are started so that there won’t be a race condition between threads calling getcontext(). It then has this example: # Set applicationwide defaults for all threads about to be launched DefaultContext.prec = 12 DefaultContext.rounding = ROUND_DOWN DefaultContext.traps = ExtendedContext.traps.copy() DefaultContext.traps[InvalidOperation] = 1 setcontext(DefaultContext) # … -
Is Django best framework for your web application development
What is Django? Django is a Python Framework or in easy words, it is an online development framework. Django is a high-level associate that has an MVC-MVT titled design. Django framework is written on fast and powerful python language Django has an associate ASCII text file assortment of libraries for building a completely functioning net application What is A Framework? A Framework gives a structure and basic techniques to make the life of a web application engineer a lot simpler for structure adaptable, versatile and viable web applications Why Django framework? Django is viewed as the best Python web system, and it's incredible for making database-driven sites A Python web system is a code library that gives instruments and libraries to disentangle normal web improvement tasks Django is associate degree open supply framework, conjointly one among the foremost standard open supply frameworks permits programmers to scale Python comes. Django is constructed on the principle of model read model subject pattern (MVT) and it helps developers build a complicated database-driven websites. With regards to web advancement, the structure you choose to utilize is crucial. Django is a free, open source, a top of the line structure written in the broadly basic, … -
How to Upload a File Using Django REST Framework
When you develop a web app or a mobile app with Django, it is common to use the Django REST Framework for communication with the server-side. The client-side makes GET, POST, PUT, and DELETE requests to the REST API to read, create, update, or delete data there. The communication by Ajax is pretty uncomplicated, but how would you upload an image or another file to the server? I will show you that in this article by creating user avatar upload via REST API. Find the full code for this feature on Github. Extend Django User model We will start by installing Pillow for image handling to the virtual environment using the standard pip command: (venv)$ pip install Pillow Create accounts app with a custom User model: # myproject/apps/accounts/models.pyimport osimport sysfrom django.db import modelsfrom django.contrib.auth.models import AbstractUserfrom django.utils import timezonefrom django.utils.translation import gettext_lazy as _def upload_to(instance, filename): now = timezone.now() base, extension = os.path.splitext(filename.lower()) milliseconds = now.microsecond // 1000 return f"users/{instance.pk}/{now:%Y%m%d%H%M%S}{milliseconds}{extension}"class User(AbstractUser): # … avatar = models.ImageField(_("Avatar"), upload_to=upload_to, blank=True) You can add there as many fields as you need, but the noteworthy part there is the avatar field. Update the settings and add the accounts app to INSTALLED_APPS, set the AUTH_USER_MODEL, … -
Managing state in Django models
A Django model often will contain some form of state. And there will most likely be events which modify the state. While those two things are not always called like this, the concept still exists. More often than not you will find a view controller assigning the new state to an instance of a model when certain conditions are met. In some cases you will see the event code being abstracted to make it easier to test, maybe in a separate function or class, maybe to a model method. And more often than you should you will see this messing up the state of an instance forcing someone to wake up at 2am to connect to the database - or Django admin if setup - and set a valid state. Thankfully the chance for the last thing to happen can be greatly reduced. Over the years I found that not everyone is familiar with the concept of a finite-state machine (which would help addressing this problem). I have also met people who had in depth knowledge of state machines in all their forms and would have been able to implement one as a stand-alone system, but had trouble integrating one … -
Django News - Issue 15 - Mar 20th 2020
News DjangoCon Europe 2020 is postponed to September 16-20 Current Covid-19 peak estimates are too close to the original dates (May 27-31), we can not delay making a decision anymore. Some conferences are postponing, others canceling altogether. djangocon.eu Google Summer of Code - Apply by March 31 Students can apply to spend 3 months working on an open source project, including Django! Last year’s GSOC student contributed JSON Field support which is a forthcoming major feature in Django 3.1. withgoogle.com Async views will be in Django 3.1! Check out the pull request to see the fantastic work by Andrew Godwin and the Django team. github.com Articles Google Summer of Code Reflection Fantastic advice from Django's 2019 GSOC student. laymonage.com Use `python -m pip` everywhere Wise advice from Adam Johnson on managing Python dependencies. adamj.eu Against service layers in Django An in-depth piece on structuring code and logic within Django. b-list.org The most critical Python code metric A quick and dirty way to evaluate the quality of any Python code. stevedower.id.au How a request becomes a response: Diving deeper into WSGI Part of an excellent series on Django and webpage internals. djangodeconstructed.com Understanding many to one in Django A deep dive … -
Customizing Django Authentication using AbstractBaseUser
Introduction Django has a builtin app that provides much of the required authentication machinery out of the box. It also allows you to customize authentication if the defaults don’t match your requirements. There are multiple approaches you can take including: Using a Proxy Model based on User. Adding a OneToOneField that points to User on a Profile model. Extending AbstractUser and adding fields for additional profile information. Extending AbstractBaseUser ... -
Managing state in Django models
Managing state in Django models A Django model often will contain some form of state. And there will most likely be events which modify the state. While those two things are not always called like this, the concept still exists. More often than not you will find a view controller assigning the new state to an instance of a model when certain conditions are met. In some cases you will see the event code being abstracted to make it easier to test, maybe in a separate function or class, maybe to a model method. And more often than you should you will see this messing up the state of an instance forcing someone to wake up at 2am to connect to the database - or Django admin if setup - and set a valid state. Thankfully the chance for the last thing to happen can be greatly reduced. Over the years I found that not everyone is familiar with the concept of a finite-state machine (which would help addressing this problem). I have also met people who had in depth knowledge of state machines in all their forms and would have been able to implement one as a stand-alone system, … -
We're Now a Little Company Called Feldroy
We discovered that our branding as "Roy Greenfeld" was causing confusion. People came and thought we were some guy named Roy, since Roy is a common American first name. Furthermore, our last name of "Roy Greenfeld" had been causing errors in systems that don't handle spaces well. I posted about our name confusion and system error dilemma on dev.to, and the DEV community came up with amazing solutions for us. The turning point of the discussion there was: "Why not mix it up with Feldroy? Seems cool and science-fiction-y." -- Daniel Starner "+1 for Feldroy. Feels like the name of a hypothetical protagonist of a book by Aldous Huxley" -- rhymes Well, Daniel Roy Greenfeld and I discussed it, and now we're Daniel Feldroy and Audrey Feldroy. Naturally, our little company is now called Feldroy too. It'll take some time to get all the paperwork filed, which is a bit tricky at the moment given that Los Angeles City Hall is closed due to the COVID-19 outbreak. In the meantime, we're updating our profiles and our website branding to call ourselves Feldroy. There may be a few hiccups while the DNS records propagate and we trawl everything manually to update … -
We're Now a Little Company Called Feldroy
We discovered that our branding as "Roy Greenfeld" was causing confusion. People came and thought we were some guy named Roy, since Roy is a common American first name. Furthermore, our last name of "Roy Greenfeld" had been causing errors in systems that don't handle spaces well. I posted about our name confusion and system error dilemma on dev.to, and the DEV community came up with amazing solutions for us. The turning point of the discussion there was: "Why not mix it up with Feldroy? Seems cool and science-fiction-y." -- Daniel Starner "+1 for Feldroy. Feels like the name of a hypothetical protagonist of a book by Aldous Huxley" -- rhymes Well, Daniel Roy Greenfeld and I discussed it, and now we're Daniel Feldroy and Audrey Feldroy. Naturally, our little company is now called Feldroy too. It'll take some time to get all the paperwork filed, which is a bit tricky at the moment given that Los Angeles City Hall is closed due to the COVID-19 outbreak. In the meantime, we're updating our profiles and our website branding to call ourselves Feldroy. There may be a few hiccups while the DNS records propagate and we trawl everything manually to update … -
Onboarding - Building SaaS #48
In this episode, we did some design work to plan out the onboarding flow for new users. I spoke through my design process, outlined what a new user will need to do to succeed, wrote down the plan in GitHub issues, then started to implement that flow. I started the stream with a quick fix to the main app view. After that, we started a new project in the app. I needed to design the starting experience for a user. -
Remote Work
New Django Governance ModelJohn Cleese on CreativityJohn Mulaney SNL MonologueWhy Working from home is Both Awesome and Horrible -
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 -
Against service layers in Django
Recently I’ve seen posts and questions pop up in a few places about a sort of “enterprise” Django style guide that’s been getting attention. There are a number of things I disagree with in that guide, but the big one, and the one people have mostly been asking about, is the recommendation to add a “service layer” to Django applications. The short version of my opinion on this is: it’s probably not what you want … Read full entry -
Use Pathlib in Your Django Settings File
Django’s default settings file has always included a BASE_DIR pseudo-setting. I call it a “pseudo-setting” since it’s not read by Django itself. But it’s useful for configuring path-based settings, it is mentioned in the documentation, and some third party packages use it. (One that I maintain, the Scout APM Python integration, uses it.) Django has, up until now, defined it as: import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) This changes in version 3.1, which as I write is still months in the future. Thanks to a contribution by Jon Dufresne and Curtis Maloney, it’s instead defined using pathlib: from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve(strict=True).parents[1] pathlib was added to Python’s standard library in Python 3.4, thanks to PEP 428. All file-path using functions across Python were then enhanced to support pathlib.Path objects (or anything with a __fspath__ method) in Python 3.6, thanks to PEP 519. pathlib is great! It has an easier API than os.path.join(), allows method chaining, and handles path normalization automatically. See how you can define a subdirectory using BASE_DIR / 'subdir'. If you want to read more, see … -
Layoffs are Coming
It’s looking increasingly likely that the COVID-19 pandemic will cause a recession. It’s easy to think we might be immune from the effects of a global recession, but my experience is that tech companies are quick to cut staff, especially engineers, in the face of declining markets. I hope I’m wrong, but I don’t think I am. Either way, it’s not going to hurt to prepare. -
Understanding many to one in Django
What is a many to one relationship? In database design many to one (or one to many) refers to a relationship between one or more entities where a single entity can have many entities connected. Let's make an example. Consider two entities: Mother and Daughter. As we know from real life, these two entities are most of the times connected like so: Every Daughter has one Mother One Mother can have many Daughters So we have a many to one here where many is the Daughter and one is the Mother. In an Entity Relationship Diagram this relationship is described with two boxes connected by the popular crow's foot: On the Mother side you can see two lines which stand for one and only one. That's the "one side" of the relationship. On the Daughter side instead you can see the crow's foot and a circle. The circle means optional, that is, there could be zero, one, or many Daughters. Technically a Mother should have at least one Daughter to qualify for her status, but I wanted to show you the circle and I took this liberty. A crow's foot with a straight line on top instead means at least … -
Django News - Issue 14 - Mar 13th 2020
News The Django Project adopts a new governance model The Django Project is switching from a core team to a Django Technical Board. Find out what this means and how the project will be governed. Django co-creator and former BDFL, Jacob Kaplan Moss, wrote Django’s new governance model to share his thoughts. djangoproject.com PyCon US 2020 has been canceled due to COVID-19 The Foundation is currently exploring options to re-schedule or cancel the conference. Any final decision to cancel or reschedule PyCon US will be made early enough to give you time to cancel or defer your travel plans. blogspot.com Python Insider: Python 3.7.7 is now available Python 3.7.7 is out! blogspot.com Articles About Removing Duplicates in the Database - DjangoTricks An elegant code snippet for removing duplicates in a database. djangotricks.com 9 Django Tips for Working with Databases Multiple tips for getting more out of Django's ORM. works-hub.com Django Best Practices - Function-Based Views vs Class-Based Views An overview of the pros/cons of each approach. learndjango.com Django's Field Choices Don't Constrain Your Data An interesting take on Field.choices in Django. adamj.eu TLDR: Generate Django Secret Key This TLDR is a quick reminder of how to generate a secret key … -
Django's new governance model
Starting today, Django has a new governance model. Previously, a small “core team” made most decisions, including electing a Technical Board to own decisions about each release. Now, the “core team” is gone; all power rests with the Technical Board. Anyone who’s made substantial contributions to Django is now eligible to run, and the board is now elected by the DSF Membership at large. You can read more about the change in today’s announcement, and if you want to full details they’re in DEP 10. -
API-First Architecture
Weekly Django Chat NewsletterDjango for APIs book Official Django REST Framework Tutorial - A Beginner's GuideAymeric Augustin 3-part series DRF Authentication Docs DjangoCon US 2018 - Finally Understand Authentication in Django REST Framework LearnDjango - The 10 Most-Used Django PackagesLearnDjango - Essential Django 3rd Party Packages