Django community: Community blog posts RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Mercurial mirror for Django 3.1 branch
As usual when Django releases a new Alpha version, I’ve settled a new mirror. The URL (both web and clone) is https://hg.freehackers.org/mirrors/django-3.2-production As stated before, I no longer use bitbucket as they’ve dropped support for mercurial. Shame on them ! 🙂 -
Simple In-Memory Caching of Django Model Data With cachetools
A client project recently was suffering from an N+1 queries problem in a complicated Django admin page. Many measures had already been taken to prevent N+1 queries, such as use of django-auto-prefetch and some manually tuned select_related() / prefetch_related() calls. The remaining N+1 in question was a bit resistant to those methods because it came through several layers of admin code and many-to-many fields, making it harder than normal to find the place to modify the QuerySet construction. Instead of spelunking through those layers of code to fix this N+1, I took a “shortcut” with some local in-memory caching. This was possible because of the particular model. The model in question represents a county in the United States: class County(auto_prefetch.Model): state = auto_prefetch.ForeignKey(State, on_delete=models.CASCADE) name = models.CharField(max_length=30) ... def __str__(self): return f"{self.name}-{self.state.abbreviation}" The N queries came from the __str__() method, which is used to display a given County in the admin. A list of N counties was selected from a related model without using select_related() or prefetch_related() to select the related states at the same time. Therefore Django performed an extra query to select the state when calling __str__() on each county. The page in question displayed a list of … -
Customer Requests - Building SaaS #88
In this episode, I worked on some customer requests now that I’ve finished launching the product. These requests improved the usability of the application in a few spots that were lacking. The first request from the customer was to make it clear on the daily view when tasks are graded or not. Before I could change the template, I need to add a new method to the CourseTask to check if the task is graded. -
Django Girls - Rachell Calhoun
RachellCalhoun.comDjangoGirls.orgGitHub profileToptal scholarship winnerDjango Girls new leadershipDjangoCon US 2016: Git in Control, Version Control and How it Could... by Rachell CalhounSupport the ShowThis podcast is a labor of love and does not have any ads or sponsors. To support the show, consider purchasing or recommending a book from LearnDjango.com or signing up for the free weekly Django News newsletter. -
Efficient Reloading in Django’s Runserver With Watchman
If you start the development server on a Django project, it looks something like this: $ python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (1 silenced). January 20, 2021 - 04:25:31 Django version 3.1.5, using settings 'db_buddy.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. When you make changes to a Python file, the server automatically reloads. This is powered by a file watcher, and Django reports which one it’s using in the first line, defaulting to StatReloader. The StatReloader class is simple but reliable. It works by running a loop that checks all your files for changes every second - this is pretty inefficient, especially as your project grows! A lesser-known but better alternative is to use Django’s support for watchman. Support was added in Django 2.2, thanks to Tom Forbes in Ticket #27685. Watchman is an efficient file watcher open sourced by Facebook. It works by receiving file change notifications from your operating system and bundling them together. When nothing is changing, it doesn’t need to do any work - saving processing power and consequently laptop battery life. And when something changes, Django gets notified about it … -
Efficient Reloading in Django’s Runserver With Watchman
If you start the development server on a Django project, it looks something like this: $ python manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (1 silenced). January 20, 2021 - 04:25:31 Django version 3.1.5, using settings 'db_buddy.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. When you make changes to a Python file, the server automatically reloads. This is powered by a file watcher, and Django reports which one it’s using in the first line, defaulting to StatReloader. The StatReloader class is simple but reliable. It works by running a loop that checks all your files for changes every second - this is pretty inefficient, especially as your project grows! A lesser-known but better alternative is to use Django’s support for watchman. Support was added in Django 2.2, thanks to Tom Forbes in Ticket #27685. Watchman is an efficient file watcher open sourced by Facebook. It works by receiving file change notifications from your operating system and bundling them together. When nothing is changing, it doesn’t need to do any work - saving processing power and consequently laptop battery life. And when something changes, Django gets notified about it … -
Scheduling Celery Tasks in the (far) future
We used to make use of the fact that a celery task can be scheduled at some time in the future to auto-punch-out staff members who failed to punch out 24 hours after their shift started. This was as simple as scheduling a task with an `eta=86400`. However, as Adam points out [here](https://adamj.eu/tech/2020/02/03/common-celery-issues-on-django-projects/) (see number 5). This is not a great idea. For one, it will bog down your celery workers (and make them use a bunch more memory). Secondly, something Adam doesn't mention is that if your queue is corrupted, then all of those future tasks will not be executed. Discussing this in IRC today, I thought of a simple mechanism for scheduling these tasks and processing them at some point after they are due. We will start with a model to store our task: {% highlight python %} class ScheduledTask(models.Model): task_name = models.TextField() task_args = models.JSONField(default=list) task_kwargs = models.JSONField(default=dict) due_date = models.DateTimeField() objects = ScheduledTaskQuerySet.as_manager() @property def task(self): module, task = task_name.rsplit('.', 1) return getattr(importlib.import_module(module), task) def execute(self): self.task.apply_async(args=self.args, kwargs=self.kwargs) {% endhighlight %} We have a custom queryset defined, that allows us to see which tasks are due using a nice queryset method: {% highlight python %} class … -
Scheduling Celery Tasks in the (far) future
We used to make use of the fact that a celery task can be scheduled at some time in the future to auto-punch-out staff members who failed to punch out 24 hours after their shift started. This was as simple as scheduling a task with an `eta=86400`. However, as Adam points out [here](https://adamj.eu/tech/2020/02/03/common-celery-issues-on-django-projects/) (see number 5). This is not a great idea. For one, it will bog down your celery workers (and make them use a bunch more memory). Secondly, something Adam doesn't mention is that if your queue is corrupted, then all of those future tasks will not be executed. Discussing this in IRC today, I thought of a simple mechanism for scheduling these tasks and processing them at some point after they are due. We will start with a model to store our task: {% highlight python %} class ScheduledTask(models.Model): task_name = models.TextField() task_args = models.JSONField(default=list) task_kwargs = models.JSONField(default=dict) due_date = models.DateTimeField() objects = ScheduledTaskQuerySet.as_manager() @property def task(self): module, task = task_name.rsplit('.', 1) return getattr(importlib.import_module(module), task) def execute(self): self.task.apply_async(args=self.args, kwargs=self.kwargs) {% endhighlight %} We have a custom queryset defined, that allows us to see which tasks are due using a nice queryset method: {% highlight python %} class … -
[Django]Composite Primary Key
0.Do Django models support composite primary key? https://docs.djangoproject.com/en/3.1/faq/models/#do-django-models-support-multiple-column-primary-keys https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys Django doesn’t officially support composite primary key. If there is no field for which “primary_key = True” is specified, “id” will be automatically added as the primary key. So if you are using the DB of the conventional system, “update” and “delete” method do not work. You have to add “id” field to your tables or to make your own SQL query. 1.Solutions Proposal1:Add a surrogate key If I add a surrogate key (‘id’) to the DB and put a unique constraint (unique_together) on the composite key, I can use the Django model as it is. (Problems) DB rebuild takes time and requires planned outage. Proposal2:Implement with your own query Make my own query-write implementation without relying on ORM. I can control the performance of queries by yourself. (Problems) More code and testing. Proposal3:Extend Django Model It seems that it was also considered in the Django project, but I’m not sure recent status. https://code.djangoproject.com/ticket/373 https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys#CurrentStatus It seems that some people have tried various ways, but in the end, are they all unfinished? For the time being, by extending the model myself, create a subclass of Model so that I can use … -
Adding Social Authentication to Django
This tutorial details how to set up social auth with Django and Django Allauth. -
How to gather consensus before a big decision
The next time you have an important proposal to make, don’t wait until the big meeting to ask for support. Here’s how to gather feedback and build consensus beforehand, so you can make that big meeting into a non-event. -
How to implement Auto Expiring Token in Django Rest Framework
Describes how to implement custom token model and authentication backend in Django Rest Framework -
How to implement Auto Expiring Token in Django Rest Framework
Describes how to implement custom token model and authentication backend in Django Rest Framework -
Django News - CSS, PostgreSQL, Django Security with CI/CD, and more htmx resources - Jan 15th 2021
News The State of CSS 2020: Trend Report CSS is rapidly changing with utility-first frameworks like Tailwind CSS gaining rapid prominence. This is a fun, interactive look at a recent survey on current and future CSS trends. stateofcss.com Articles Bringing Security along on the CI/CD journey From Jacob Kaplan-Moss, a detailed look at how to integrate security with modern CI/CD practices. jacobian.org Re-Introducing Hash Indexes in PostgreSQL by Haki Benita A very in-depth look at hash indexes within PostgreSQL and why they are more relevant than ever these days. hakibenita.com Detecting N+1 queries in Django with unit testing A clever way to unit test for N+1 queries in your code. valentinog.com Interview with Andrew Godwin about deployment — Django deployment From 2017 but still very relevant, an interview with on Django deployments with Andrew Godwin. djangodeployment.com Django migrations without downtimes Originally from 2015 but updated a few times since then, this is a classic look at managing Django migrations on a large site without downtimes. github.io How to Override the gunicorn Server Header by Adam Johnson Adam Johnson shows how to override Gunicorn's header which, by default, reports the complete version of Gunicorn being used: a security no-no. adamj.eu Sponsored … -
Hangar's Dumb Security Questionnaire
Over on the Hangar tech blog, I’ve posted our Dumb Security Questionnaire (the questions we ask vendors to evaluate their security maturity). All DSQs are dumb, but I think ours is a little less dumb. If not, at least it’s short. -
Squashing Bugs - Building SaaS #87
In this episode, I fixed some critical issues that my customer discovered. My customer is putting the app through its real paces for a school year and since this is the first run, there were bound to be some bugs. We began with an explanation of the issues that my customer encountered. The problems related to scheduling. First, the daily page skipped a task and showed the task that was meant for two days in the future. -
Why I'm excited to get a COVID vaccine
I’m terrifically excited to get a COVID vaccine. Not just “willing”, but actually quite excited. mRNA vaccines are this generation’s moonshot – a huge leap forward in science. Im terrifically excited to get to be part of it. -
How to accept Paypal payments on your Django application
If you are developing a Django application and you want to accept Paypal payments, you are in the right place! In this tutorial you’ll learn how to integrate Django and Paypal to accept payments in your Django web application. Paypal is a popular solution for online payments. Even if nowadays there are many valid alternatives, Paypal is still a big player in the market with a solid reputation and it’s trusted by millions of users. Here is a simple step-by-step guide on how to integrate the django-paypal third party app on your website. 1. Install the django-paypal app pip install django-paypal 2. Insert the app in your Django settings INSTALLED_APPS = ( ... 'paypal.standard.ipn', ) 3. Insert a Paypal form on a view Assuming you are using Django class-based views, you can use a FormView like this: from django.views.generic import FormView from django.urls import reverse from paypal.standard.forms import PayPalPaymentsForm class PaypalFormView(FormView): template_name = 'paypal_form.html' form_class = PaypalForm def get_initial(self): return { "business": 'your-paypal-business-address@example.com', "amount": 20, "currency_code": "EUR", "item_name": 'Example item, "invoice": 1234, "notify_url": self.request.build_absolute_uri(reverse('paypal-ipn')), "return_url": self.request.build_absolute_uri(reverse('paypal-return')), "cancel_return": self.request.build_absolute_uri(reverse('paypal-cancel')), "lc": 'EN', "no_shipping": '1', } This is a regular FormView and the template paypal_form.html is a standard Django template like this: … -
How to accept Paypal payments on your Django application
In this tutorial you’ll learn how to integrate Django and Paypal to accept payments in your Django web application. Paypal is a popular solution for online payments. Even if nowadays there are many valid alternatives, Paypal is still a big player in the market with a solid reputation and it’s trusted by millions of users. Here is a simple step-by-step guide on how to integrate the django-paypal third party app on your website. Table of Contents 1. Install the django-paypal app 2. Insert the app in your Django settings 3. Insert a Paypal form on a view 4. Provide an URL for Paypal IPN 5. Create views for success and failure of Paypal checkout 6. Setup a listener to detect successful Paypal payments 1. Install the django-paypal app pip install django-paypal 2. Insert the app in your Django settings INSTALLED_APPS = ( ... 'paypal.standard.ipn', ) 3. Insert a Paypal form on a view Assuming you are using Django class-based views, you can use a FormView like this: from django.views.generic import FormView from django.urls import reverse from paypal.standard.forms import PayPalPaymentsForm class PaypalFormView(FormView): template_name = 'paypal_form.html' form_class = PayPalPaymentsForm def get_initial(self): return { "business": 'your-paypal-business-address@example.com', "amount": 20, "currency_code": "EUR", "item_name": 'Example item, … -
Here, There, Middleware
Full show notes are available at https://www.mattlayman.com/django-riffs/11. -
Episode 11 - Here, There, Middleware
On this episode, we will investigate Django middleware and see where it goes in your project. In the process, you’ll see why middleware is useful and how you can work with it. Listen at djangoriffs.com. Last Episode On the last episode, we’re going to look at working with users in a Django project. We’ll see Django’s tools for identifying users and checking what those users are permitted to do on your website. -
Bringing Security along on the CI/CD journey
Practical ways to bridge the gap between AppSec and Engineering. -
Using MinIO to upload to a local S3 bucket in Django
Hi everyone! Some weeks ago I was doing a demo to my teammates, and one of the things that was more suprising for them was that I was able to do S3 uploads locally using “MinIO”. Let me set the stage: Imagine you have a Django ImageField which uploads a picture to a AWS S3 bucket. How do you setup your local development environment without using a “development” AWS S3 Bucket? -
Using MinIO to upload to a local S3 bucket in Django
Hi everyone! Some weeks ago I was doing a demo to my teammates, and one of the things that was more suprising for them was that I was able to do S3 uploads locally using “MinIO”. Let me set the stage: Imagine you have a Django ImageField which uploads a picture to a AWS S3 bucket. How do you setup your local development environment without using a “development” AWS S3 Bucket? For that, we use MinIO. What is MinIO? According to their GitHub README: > MinIO is a High Performance Object Storage released under Apache License v2.0. It is API compatible with Amazon S3 cloud storage service. So MinIO its an object storage that uses the same API as S3, which means that we can use the same S3 compatible libraries in Python, like Boto3 and django-storages. The setup Here’s the docker-compose configuration for my django app: version: "3" services: app: build: context: . volumes: - ./app:/app ports: - 8000:8000 depends_on: - minio command: > sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000" minio: image: minio/minio ports: - 9000:9000 environment: - MINIO_ACCESS_KEY=access-key - MINIO_SECRET_KEY=secret-key command: server /export createbuckets: image: minio/mc depends_on: - minio entrypoint: > /bin/sh -c " … -
About Print Versions of "A Wedge of Django"
Due to a never ending set of logistical issues for the print versions of our Django Crash Course, A Wedge of Django, we have decided to cancel our attempt to handle printing it ourselves. This will free us up in order to finish it, as well as other books. If you pre-ordered a print version of this book we have sent out emails asking if you want a refund or discount code for other purchases on this site, including the e-book version of Two Scoops of Django. If you have not received this email and pre-ordered the book, please contact us at hi @ feldroy.com. Finally, there is a significant possibility we will use Amazon's print-on-demand service to publish the book, in which case it will have to be purchased as an Amazon product. Sincerely,Daniel, Audrey, and Uma