01 Jul 2022

feedDjango community aggregator: Community blog posts

Django News - 2022 Campaign Results - Jul 1st 2022

News

PyCharm & DSF Campaign 2022 Results

The sixth annual JetBrains PyCharm promotion in June netted the Django Software Foundation $25,000 this year.

djangoproject.com

🚨 Django 4.0.6 and 3.2.14 high severity security releases on July 4th

Mariusz Felisiak gave everyone a heads up to look for a Django 4.0.6 and 3.2.14 high severity security update on July 4th.

twitter.com

Sponsored Ad

Django GDPR Cookie Consent

Highly customizable Django app to make your Django website compatible with GDPR Cookie law.

gumroad.com

Articles

Forms in Django 4.0+

Django 4.0 made rendering forms more flexible using the template engine. David Smith shows us how to render forms and highlights what features to expect in Django 4.1.

github.io

How to set up Webpack and TailwindCSS in a Django Project

A guide to one-time guide setting up Webpack and Tailwind CSS to work with a Django application.

builtwithdjango.com

How to Run a Django Migration "By Hand"

Adam Johnson shows us how to apply database migrations by hand using sqlmigrate and dbshell.

adamj.eu

Google Summer of Code 2022: One Month Progress

The first post by one of this year's Google Summer of Code recepients, Deepak Dinesh, who is focusing on improving benchmarking within Django.

medium.com

Videos

Python Web Conf 2022 Talks + Tutorials

All 90 videos from the 2022 Python Web Conf are now available on YouTube.

youtube.com

Securing Django Applications by Gajendra Deshpande

A talk on performing penetration testing on Django web applications as well as strategies and configuration settings for making the source code and Django applications secure.

youtu.be

Make the Most of Django by Paolo Melchiorre

A talk on how to take full advantage of Django while contributing to its success and that of its community.

youtu.be

Sponsored Link

Fit Django perfectly with Kubernetes

Hurricane is an initiative to fit Django perfectly with Kubernetes. It allows you to make use of many django standard features to leverage the capabilities of Kubernetes to its fullest extent.

django-hurricane.io

Projects

Bunny Fonts

Bunny Fonts are GDPR friendly and an alternative to services like Google Fonts.

bunny.net

Salaah01/django-form-creator

A Django app that allows users to dynamically create forms.

github.com

strawberry-graphql/strawberry-graphql-django: Strawberry GraphQL Django extension

Strawberry GraphQL Django extension.

github.com

Sponsorship

📰 Sponsor Django News

Want to get your product, service, job, or company in front of over 2,690 Django professionals each week?

We have some summer sponsorship inventory left and would love to feature you. Sponsorship availability, prices, and details are available here.

django-news.com


This RSS feed is published on https://django-news.com/. You can also subscribe via email.

01 Jul 2022 3:00pm GMT

29 Jun 2022

feedDjango community aggregator: Community blog posts

How to Run a Django Migration “By Hand”

Normally your Django project's deploy process runs the migrate command, and that takes care of updating your database as necessary. Especially on smaller databases, Django's migration system can "just do it" for you.

But sometimes it can be necessary to run migrations "by hand" in your database's SQL console. I have found this to be the case with larger, busy databases, and when using tools like pt-online-schema-change to apply schema changes. In this post we'll cover the process for running a migration by hand, and adapting it to reversing migrations.

Run a Migration "By Hand"

Hold onto your butts…

1. Find the SQL to run

We write Django migrations in Python, but they ultimately end up running a series of SQL statements. To run a migration by hand, you need those SQL statements, so you can run them yourself.

You can display the SQL for a migration with Django's sqlmigrate command like:

$ ./manage.py sqlmigrate <app> <prefix>

This outputs each migration operation with a commented header describing what it does and then its actual statements. Replace <app> with the label of the app that the migration lives in. Replace <prefix> with a unique prefix of the migration's name - normally a four digit number like 0003.

For example, to show the SQL for the core app's migration with name starting "0003", you would run:

$ ./manage.py sqlmigrate core 0003
BEGIN;
--
-- Add field page_count to book
--
ALTER TABLE "core_book" ADD COLUMN "page_count" integer NULL;
COMMIT;

You'll only see the bookending with BEGIN and COMMIT on databases that support transactional schema changes (SQLite and PostgreSQL, of Django's built-in backends). These may be disabled per-migration, when necessary.

If any operations cannot be run as SQL, they will have the message THIS OPERATION CANNOT BE WRITTEN AS SQL (from Django 4.1, older versions have slightly different wording). This normally means use of the RunPython operation. For such operations, will need to figure out how to run them by hand separate to the process I'm covering here. You could also consider splitting them into their own migration.

A small note: it's best to run sqlmigrate against your production database. For certain operations, Django queries the database to find names of objects, patricularly index names when migrating older index definitions. Depending on how your various environments' databases were created and migrated over time, these names can be different. Thus, the SQL that Django generates on your staging server may be different to that on production. But on the other hand, using your production settings with an unexecuted migration isn't always easy, so you may just want to beware of this issue and adjust SQL when necessary.

2. Execute the SQL statement-by-statement

Open up your database's SQL shell on the target environment with Django's dbshell command:

$ ./manage.py dbshell

Here you can run the migration's SQL statements from sqlmigrate, one by one. Skip the comments from sqlmigrate (the lines starting --), and make sure you copy whole SQL statements that end with ;.

For example, running the above on PostgreSQL:

$ ./manage.py dbshell
psql (14.4, server 13.5 (Debian 13.5-1.pgdg110+1))
Type "help" for help.

example=# BEGIN;
BEGIN
example=*# ALTER TABLE "core_book" ADD COLUMN "page_count" integer NULL;
ALTER TABLE
example=*# COMMIT;
COMMIT
example=#

Whilst running the migration, you should keep an eye on your database's key metrics with whatever monitoring tools you use. You may also want to use a second dbshell to run some administrative commands, for example in my recent PostgreSQL post I covered finding and stopping queries that block an ALTER TABLE.

One note for databases supporting transactional schema changes (SQLite, PostgreSQL). If the migration has a single schema-changing statement like ALTER TABLE, you can drop BEGIN and COMMIT. This means less SQL to run, and you'll hold schema locks for slightly less time, reducing the risk of the migration affecting your busy production database.

3. Record migration as executed

After you've run your migration's statements by hand, you need to record the migration as executed. If you don't, the migrate command will try to again execute that migration, which could end catastrophically (but will normally just result in an error like "column already exists").

Django's migration systems keeps a record of executed migrations in a table called django_migrations. You can record a migration as executed with this query template:

INSERT INTO django_migrations (app, name, applied) VALUES (<app>, <name>, NOW());

Replace <app> with the label of the app that the migration lives in. Replace <name> with the full name of the migration, which is the migration's filename without the .py extension. (Be careful not to use just a prefix like 0003!)

For example, to mark our example migration as complete:

example=# INSERT INTO django_migrations (app, name, applied) VALUES ('core', '0003_book_page_count', NOW());
INSERT 0 1

You can check your entry looks right by visually comparing it with others:

SELECT * FROM django_migrations ORDER BY applied DESC;

…and just like that, you're done applying the migration!

Reverse a Migration "By Hand"

You can use the above process to reverse a migration "by hand" with a couple of changes.

In step one, use the --backwards flag to sqlmigrate to generate the SQL statements to undo the migration. Use this template:

$ ./manage.py sqlmigrate --backwards <app> <prefix>

And in step three, you'll want to delete from, rather than insert into, the django_migrations table. Use this template:

DELETE FROM django_migrations WHERE app = <app> AND name = <name>;

Hopefully you don't need to roll back migrations often!

Fin

May all your migrations run smoothly,

-Adam

29 Jun 2022 4:00am GMT

24 Jun 2022

feedDjango community aggregator: Community blog posts

Django News - Django 4.1 beta 1 released - Jun 24th 2022

News

Django 4.1 beta 1 released

Django 4.1 beta 1 is now available. It represents the second stage in the 4.1 release cycle and is an opportunity for you to try out the changes coming in Django 4.1.

djangoproject.com

2022 Python Software Foundation Board Elections

Just a reminder that if you are a registered member of the Python Software Foundation, you may vote in this year's election until June 30th, 2022 AoE.

python.org

Get paid to contribute to urllib3

Announcing urllib3's bounty program.

sethmlarson.dev

Sponsored Ad

Affordable Heroku-like experience for Django.

Spin up a production-ready Django application, Postgres DB, and Redis in 5 minutes on AWS, DO, or any other provider.

appliku.com

Articles

Customizable Django Admin themes

Arpit shows us how to quickly use django-admin-interface to customize the Django Admin's color scheme and how to load custom themes.

dev.to

Six things I do every time I start a Django project

Everyone approaches starting a project differently, and this was a fun read to see how Brenton Cleeland does it. This topic might make a fun Django Forum thread too.

brntn.me

How to Find and Stop Running Queries on PostgreSQL

A look at stopping PostgreSQL queries via SQL, techniques for finding problematic queries, and the occasionally useful ability to cancel via operating system tools.

adamj.eu

Don't Mock What You Don't Own in 5 Minutes

One of the most common issues programmers have, when they try to test real-world software is how to deal with third-party dependencies.

hynek.me

Why new Macs break your Docker build, and how to fix it

On new Macs, many Python-based Dockerfiles fail in entirely unexpected ways. A look at why this happens and potential fixes in the future.

pythonspeed.com

Podcasts

Django Chat: How to Learn Django (Ep2 Replay)

Carlton and Will discuss how to learn Django whether you are a total beginner or experienced web developer.

djangochat.com

Sponsored Link

Fit Django perfectly with Kubernetes

Hurricane is an initiative to fit Django perfectly with Kubernetes. It allows you to make use of many django standard features to leverage the capabilities of Kubernetes to its fullest extent.

django-hurricane.io

Projects

samwillis/nodejs-pypi

If you ever wondered why you couldn't pip install nodejs-bin to install Node.js in your projects, well, now you can.

github.com

dozymoe/frozen-django

frozen-django creates static websites from your Django views into HTML files.

github.com


This RSS feed is published on https://django-news.com/. You can also subscribe via email.

24 Jun 2022 3:00pm GMT