DEV Community

Jeff Triplett (he/him) for Django News

Posted on • Originally published at django-news.com on

Django News #12 - Tips on testing, templates, and Django within healthcare

News

Python Insider: Python 3.8.2 and 3.9.0a4 are now available

Python 3.8.2 contains numerous new bug fixes while 3.9.0a4 is the fourth of six planned alpha releases leading up to its planned release in August.


Sponsor Django on Github

Django relies on community donations and now you can fund it directly via Github. Currently 40+ monthly sponsors.


Articles

Django Best Practices - Template Structure

A look at the two dominant ways to structure templates in a Django app.


The Ultimate Guide to Django Redirects – Real Python

An in-depth look at HTTP redirects and multiple ways to implement them in Django.


Automating Performance Testing in Django

A guide to spotting N+1 query issues in your Django app.


Sponsored Link

Django Crash Course: Covers Python 3.8 and Django 3.x - Alpha Version

The authors of Two Scoops of Django have released their latest book, the Django Crash Course. Based on their corporate training, they are offering the book for under US$20, which is a steal considering they normally charge $3000/person for in-person training. This book, currently 50 chapters long, is intended for passionate learners willing to get the latest, most cutting-edge written materials. Now in beta, the book is being updated several days a week!.

Available in E-Book format, the paperback, coil-bound, and hardcover versions are available for pre-order.

roygreenfeld.com


Videos

Django and DRF Testing Series

A 10-part series on testing Django and Django Rest Framework.


PyCon 2017 - Let's Build a Web Framework

Django co-creator Jacob Kaplan-Moss walks through building a web framework from scratch, and the core decisions necessary along the way.


Podcasts

Django Chat - Healthcare with Jacinda Shelly

Jacinda is the co-founder of Apero Health and formerly CTO of DoctorOnDemand. The discussion covers lessons learned building both platforms from scratch and how to scale code and teams.


Tutorials

Safely Including Data for JavaScript in a Django Template

Pro tips from core contributor Adam Johnson on how to properly use JavaScript in Django templates.


Getting Started Testing: pytest edition

Ned Batchelder, author of the popular coverage.py package, gave an updated version of his classic talk on Python testing at the most recent Boston Python meetup.


Projects

nplusone

Auto-detecting the n+1 queries problem in Python

GitHub logo jmcarp / nplusone

Auto-detecting the n+1 queries problem in Python

nplusone

Latest version Travis-CI Code coverage

nplusone is a library for detecting the n+1 queries problem in Python ORMs, including SQLAlchemy, Peewee, and the Django ORM.

The Problem

Many object-relational mapping (ORM) libraries default to lazy loading for relationships. This pattern can be efficient when related rows are rarely accessed, but quickly becomes inefficient as relationships are accessed more frequently. In these cases, loading related rows eagerly using a JOIN can be vastly more performant. Unfortunately, understanding when to use lazy versus eager loading can be challenging: you might not notice the problem until your app has slowed to a crawl.

nplusone is an ORM profiling tool to help diagnose and improve poor performance caused by inappropriate lazy loading. nplusone monitors applications using Django or SQLAlchemy and sends notifications when potentially expensive lazy loads are emitted. It can identify the offending relationship attribute and specific lines of code behind the problem, and recommend fixes for…


django-money

Proper handling of money/currency is surprisingly challenging. This package adds money fields for Django forms and models.

GitHub logo django-money / django-money

Money fields for Django forms and models.

django-money

Build Status Coverage Status Documentation Status PyPI

A little Django app that uses py-moneyed to add support for Money fields in your models and forms.

  • Django versions supported: 2.2, 3.2, 4.0, 4.1
  • Python versions supported: 3.7, 3.8, 3.9, 3.10
  • PyPy versions supported: PyPy3

If you need support for older versions of Django and Python, please refer to older releases mentioned in the release notes.

Through the dependency py-moneyed, django-money gets:

  • Support for proper Money value handling (using the standard Money design pattern)
  • A currency class and definitions for all currencies in circulation
  • Formatting of most currencies with correct currency sign

Installation

Using pip:

$ pip install django-money
Enter fullscreen mode Exit fullscreen mode

This automatically installs py-moneyed v1.2 (or later).

Add djmoney to your INSTALLED_APPS. This is required so that money field are displayed correctly in the admin.

INSTALLED_APPS = [
   ...,
   'djmoney',
   ...
]
Enter fullscreen mode Exit fullscreen mode

Model usage

Use as normal model fields:

from djmoney.models.fields
Enter fullscreen mode Exit fullscreen mode

django_coverage_plugin

Also from Ned Batchelder, the creator of the coverage package, a plugin for adding coverage.py to measure Django template execution.

GitHub logo nedbat / django_coverage_plugin

A plugin for coverage.py to measure Django template execution

Django Template Coverage.py Plugin

A coverage.py plugin to measure test coverage of Django templates.


Supported on:

  • Python: 3.7 through 3.11.
  • Django: 1.11, 2.x, 3.x and 4.x.
  • Coverage.py: 4.x or higher.

The plugin is pip installable:

$ pip install django_coverage_plugin

To run it, add this setting to your .coveragerc file:

[run]
plugins = django_coverage_plugin

Then run your tests under coverage.py.

You will see your templates listed in your coverage report along with your Python modules. Please use coverage.py v4.4 or greater to allow the plugin to identify untested templates.

If you get a django.core.exceptions.ImproperlyConfigured error you need to set the DJANGO_SETTINGS_MODULE environment variable.

Template coverage only works if your Django templates have debugging enabled If you get django_coverage_plugin.plugin.DjangoTemplatePluginException Template debugging must be enabled in settings, or if no templates get measured, make sure you have TEMPLATES.OPTIONS.debug set to True in your settings file:

TEMPLATES = [
    {
        '
Enter fullscreen mode Exit fullscreen mode

Top comments (0)