Recently I had the chance to contribute to Pretix (https://pretix.eu/about/en/) a Django-based event ticketing platform.
I submitted a couple of merge requests that were accepted: - https://github.com/pretix/pretix/pull/202/files https://github.com/pretix/pretix/pull/204/files
I will briefly detail some of the key learnings:
Django models: Being absolutely new to Django, I had the opportunity to learn along the way about one of its key features, the Models. Models are objects that the ORM will store in the database, a quick intro here: http://tutorial.djangogirls.org/en/django_orm/ It was quite surprising as well the ease of the development thanks to the automatic migrations in the database schema. More detailed info on Models here: http://www.djangobook.com/en/2.0/chapter05.html
all keyword in Python: In order to check whether a condition was True for all the elements of a list I simply iterated over them and checked for the condition:
for i, op in enumerate(positions): cancelable_products.append(op.item.allow_cancel) if False in cancelable_products: cancelable = False
However a much simpler solution making use of list comprehensions and the all() keyword in Python is possible:
cancelable = all([op.item.allow_cancel for op in positions])
Metaclasses in Python: Although I had read about Python Metaclasses in this excellent post (https://jeffknupp.com/blog/2013/12/28/improve-your-python-metaclasses-and-dynamic-classes-with-type/), Django Models are a really good practical example of their value and use. The Metaclasses are used in the Django Models to dynamically create classes.
Difference between select_related and prefetch_related in Django: prefetch_related: does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects.
select_related: limited to single-valued relationships - foreign key and one-to-one. Creates an SQL join including the fields of the related object in theSELECT
statement retrieving the related objects in the same (but more complex) database query.
By usingselect_related
instead ofprefetch_related
, one query to the DB is saved. More info on Django QuerySet: https://docs.djangoproject.com/en/1.10/ref/models/querysets/
Head over to this post for more details on Metaclasses in Django: http://reinout.vanrees.org/weblog/2013/05/16/advanced-python-metaclasses.html