Welcome to Codding Gems Collection!¶
Latest Additions¶
Django Links by ivan on 17 December
Django related links
Split CSV string into multiple rows in SQL Server (MSSQL) by ivan on 20 November
To split a string into multiple rows using a separator, use the the SPLIT_STRING table-valued function. This is very useful in situations where one column is holding a comma serpareted list of values. Another very common situation is a passing configuration from external tools - e.g. Azure Data Factory passes a list of tables to be processed.
Disable all non-clustered indexes for a table in SQL Server by ivan on 20 November
Building on Get a List of Table Indexes in SQL Server here is a SQL script which allows you to disable all non-clustered indexes for a table in SQL server. In the following example, use build a DDL statement which disables all non-clustered indexes for a table:
Get a List of Table Indexes in SQL Server by ivan on 20 November
Following SQL Server statement will return a list of all index names for a table dbo.sales.
Get the number of rows affected by the last T-SQL by ivan on 09 October
To get the number of rows affected by the last T-SQL statement in SQL server, you could youse the @@ROWCOUNT system function.
Custom Sequential Number Django Model Field by ivan on 01 October
Sometimes we need certain functionality again, and again, and again,… Sometimes it just makes sense to encapsulate certain logic into a separate module so it is easier to test and maintan. For example, we might need Django model field that is capable of keeping seqential number of the items in the order they are stored in the database.
Setting Up Fake Django Application for Testing with Pytest by ivan on 30 September
Sometimes we need to test our code in a context of a Django application. Usually this code implements generic components, e.g. custom Django fields. We could always test our components in the context of our Django application, but this breaks the requirement for test isolation. To overcome this problem we could create a Django application which is intended for testing the specific components.
Nested Serialization in Django REST Framework by ivan on 16 September
In case you are serializing a model with relationships the default ModelSerializer behavior is to provide the primary key of the relationship target. In case you need to serialize the target object not just the primary key, you have (at least) two options: explicit nested serializer and implicit nested serializer using depth.
Enable Database Lock Timeout in Django by ivan on 14 September
By default clients requesting database lock should wait until the lock is released. This might take forever. In locking contention situations it might be useful to instrument your database to timeout when lock is not being released for given amount of time, e.g. 5 seconds.
Log Django SQL Queries to Terminal by ivan on 14 September
Sometimes you might need to be able to log Django SQL queries. You might want to print them to the terminal or send them to a remote logging system. In this example I show you how to create Django middleware which logs the SQL queries executed by HTTP session to the terminal.
Register Django Model in Admin by ivan on 11 September
There are multiple methods to register a model in Django admin. I will show you how to register a single model (one by one for multiple), all the models in an application and all the models in the project.
Setup Django for Testing with pytest by ivan on 10 September
Test Driven Development is a popular software development practice which helps in creating better software. The practice that I prefer is a little bit relaxed practice that I call Test Assisted Development as sometimes tests are not written before the production code (but let’s leave this discussion for another place and time). Django projects also need to be thoroughly tested. Let’s configure our Django project for testing with pytest before even creating the first Django application.
Django Testing Cookbook by ivan on 10 September
In this cookbook we are focusing on testing Django with pytest.
Setup the Project for Django REST Framework by ivan on 09 September
Install Django REST Framework and implement Swagger interface for the API.
Generate Database Diagram from Django Models by ivan on 09 September
Database diagram are very useful project documentation. They might be useful for understanding the project. With projects under development, documentation, diagrams are not exception, easily gets out of sync with actual implementation. Generating documentation from code helps to keep the documentation always up to date with the current code.
Django REST Framework Cookbook by ivan on 09 September
This cookbook looks into working with APIs built on Django using Django REST Framework.
Django ORM Cookbook by ivan on 09 September
With this cookbook we dive into Django ORM.
Debug Django Project with Visual Studio Code (VSCode) by ivan on 09 September
In order to debug Django project, we need to first create a Visual Studio Code (VSCode) run configuration.
Setup a New Django Project by ivan on 09 September
Start a new Django project from scratch.
Core Django Cookbook by ivan on 09 September
This cookbook provides solutions for creating, managing and developing django projects.
Django Cookbook by ivan on 09 September
Welcome to this Django Cookbook (YADC - Yet Another Django Cookbook). The goal of this cookbook is to systematize and answer common questions when working with Django. The intention is to put some more advanced techniques, but to get to advanced discussions, we have to provide some context and start with some basics.