Django community: RSS
This page, updated regularly, aggregates Community blog posts from the Django community.
-
Django Tips & Tricks #8 - Hyperlink Foreignkey Fields In Admin
Consider Book model which has Author as foreignkey. from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): title = models.CharField(max_length=100) author = models.ForeignKey(Author) We can register these models with admin interface as follows. from django.contrib import admin from .models import Author, Book class BookAdmin(admin.ModelAdmin): list_display = ('name', 'author', ) admin.site.register(Author) admin.site.register(Book, BookAdmin) Once they are registed, admin page shows Book model like this. While browsing books, we can see name and author. Here, name field is liked to change view of book. But author field is shown as plain text. If we have to modify author name, we have to go back to authors admin page, search for relevant author and then change name. This becomes tedious if we spend lot of time in admin for tasks like this. Instead, if author field is hyperlinked to its change view, we can directly go to that page. Django provides an option to access admin views by its URL reversing system. For example, we can get change view of author model in book app using reverse("admin:book_author_change", args=id). Now we can use this url to hyperlink author field in book admin. from django.contrib import admin from django.utils.safestring import mark_safe class BookAdmin(admin.ModelAdmin): … -
Django Uygulamalarımızı uWSGI ve Nginx ile Google Cloud Platformdan nasıl yayınlarız? 1. Bölüm
Giriş Django, python uygulamalarımızı ve web sitelerimizi web sunuculardan yayınlamamızı sağlayan güçlü bir web framework’tur. Django ürettiğimiz kodları yerel olarak sınayabileceğimiz bir yerel sunucuyla birlikte gelir. Web sitemizi gerçek sunuculardan yayınlama esnasında bu yerel sunucu basit kalır ve güvenli değildir. Bu klavuzda Django uygulamalarımızı yayınlamak için Google Cloud Platformu kullanacağız. Bölüm’de Google Cloud Platform üzerinde bir sanal sunucu kurarak Nginx yükleyeceğiz ve makinemizi Django uygulamalarının çalışmasına hazır hale getireceğiz. Bölüm’de sunucumuzda startproject bir Django uygulaması kuracağız ve bu uygulamayı IP adresimizden yayınlayacağız. (Tüm dünya görecek) Bölüm’de uygulamamızı uWSGI yapılandırarak bir domain altında, Nginx üzerinden yayınlayacağız. (www.benimsitem.com gibi. Bölüm’de kendi makinemizde bulunan django uygulamamızı GIT deposuna atarak, sanal sunucumuz ile bu GIT deposuna bağlantı kuracak ve bir domain üzerinden uygulamamızı yayınlayacağız. Başlıyoruz… İlk işimiz Google Cloud Platformda ücretsiz hesabımızı oluşturmak. Google ilk hesap açılışlarında 300 USD bir kredi veriyor. Google hesabınızın olduğunu ve Google Cloud Platforma giriş yaptığınızı varsayıyorum. İlk işimiz Google Cloud Platformda ücretsiz hesabımızı oluşturmak. Google ilk hesap açılışlarında 300 USD bir kredi veriyor. Google hesabınızın olduğunu ve Google Cloud Platforma giriş yaptığınızı varsayıyorum. Sanal Makine Oluşturma Oluşturu tıklayıp bekliyoruz. Bu işlem bazen 10 dakika kadar sürebiliyor. Endişe etmeyin ve bekleyin. Herşey yolunda giderse karşımıza ilk makinemiz geliyor. Ben … -
How To Set Field-Level Error From Serializer Validate() Method In Django Rest Framework
Ouch, it looks like it's the longest post title of this blog! But the solution itself is pretty short and straightforward. When you raise a serializers.ValidationError, you may be tempted to provide an error as a string argument. Which works, but as a result, this error will be ... Read now -
My experience of mentoring at Django Girls Bangalore 2017
TL;DR Last Sunday, Django Girls Bangalore organized a hands-on session of web programming. This is a small event report from my side. Detailed overview Django Girls is not for a profit initiative led by Ola Sitarska and Ola Sendecka. Such movement helps women to learn the skills of website development using the well-known web-framework Django. This community is backed by organizers from many countries. Organizations like The Python Software Foundation, Github, DjangoProject and many more are funding Django Girls. Django Girls Bangalore chapter was organized by Sourav Singh and Kumar Anirudha. This was my second time mentoring for the Django Girls event. First was for the Ahmedabad chapter. The venue was sponsored by HackerEarth. 8 male and 2 female mentored 21 women during this event. Each mentor was assigned more or less 3 participants. Introducing participants with web development becomes easy with the help of Django Girls handbook. The Django Girls handbook is a combination of beginner-friendly hands-on tutorials described in a simple language. The handbook contains tutorials on basics of Python programming language to deploying your web application. Pupils under me were already ready by pre-configuring Python with their workstation. We started by introducing our selves. We took some … -
PyRoma 2017
The last of the PyRoma (Python User Group of Rome, Italy) periodic meetings in 2017 -
A Django custom command to write model fields on an Excel file
Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code. Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need. You can use the following Django custom command: The option –exclude-models can be used to specify a text file with models to exclude from Excel file (one per line). The option –include-reverse-relations (default false) can be used to include also reverse relations of your models, that are fields that are not part of the models itself, but are reverse fields from other models that have for instance a ForeignKey to the model. The command depends on the XlsxWriter package to create the Excel file. -
A Django custom command to write model fields on an Excel file
Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code. Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need. You can use the following Django custom command: https://gist.github.com/baxeico/1a06f5491f175510356359421b22cdec You can run the command like this: ./manage.py excel_models app1 app2 models.xlsx And it will write all the models of app1 and app2 on the models.xlsx file. Every model will be written on a different sheet. The option –exclude-models can be used to specify a text file with models to exclude from Excel file (one per line). The option –include-reverse-relations (default false) can be used to include also reverse relations of your models, that are fields that are not part of the models itself, but are reverse fields from other models that have for instance a ForeignKey to the model. The command depends on the XlsxWriter package to create the Excel file. The post A Django custom command to write model fields on an Excel file appeared first on Augusto Destrero - Freelance developer and sysadmin. -
A Django custom command to write model fields on an Excel file
Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code. Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need. You can use the following Django custom command: # coding=utf-8 from __future__ import unicode_literals import logging from django.core.management.base import BaseCommand, CommandError from django.apps import apps from xlsxwriter.workbook import Workbook logger = logging.getLogger(__name__) class Command(BaseCommand): help = 'Export all models of a given app to Excel file' def add_arguments(self, parser): parser.add_argument('app_labels', nargs='+', type=str) parser.add_argument('output', type=str) parser.add_argument('--exclude-models', dest="exclude_models_file", type=file) parser.add_argument('--include-reverse-relations', action='store_true', dest="include_reverse_relations", default=False) def handle(self, *args, **options): workbook = Workbook(options['output'], {'constant_memory': True}) bold = workbook.add_format({'bold': True}) headers = ['Field', 'Type'] exclude_models = [] if options['exclude_models_file']: exclude_models = options['exclude_models_file'].read().splitlines() for app_label in options['app_labels']: app_config = apps.get_app_config(app_label) app_models = app_config.get_models(include_auto_created=False) for model in app_models: model_name = model.__name__ if model_name in exclude_models: continue worksheet_name = '%s - %s' % (app_label, model_name) if len(worksheet_name) > 31: worksheet_name = '%s - %s' % (app_label[:3], model_name) worksheet = workbook.add_worksheet(worksheet_name) worksheet.write_row(0, 0, headers, bold) worksheet.set_column(0, len(headers), 30) fields = model._meta.get_fields() row = 1 for f in fields: try: fieldname = f.verbose_name except: … -
Make Your Developer's Life Easier By Reducing Number Of Opened Tabs in Pycharm
When coding, I often find myself overwhelmed by a number of opened tabs in my editor. I just seem to have opened every single .py file of a Django project. And it's kinda hard to find a way in this evergrowing of tabs. Given that, I was really happy ... Read now -
How To Get a List Of All User Permissions Available in Django Based Project
Django comes with a simple permissions system. It provides a way to assign permissions to specific users and groups of users. The system adds "change, remove and add" permissions automatically to every model class. And it's pretty easy to add custom permissions to a model class, like this: class ... Read now -
How To Show Correct List Item Indexes When Using Pagination in Django
In your Django template put something like this: <ul> {% for object in object_list %} <li>{{ forloop.counter0|add:page_obj.start_index }}. {{ object }}</li> {% endfor %} </ul> Where: object_list - is a list of objects produced by pagination; page_obj - is a page object produced by pagination page_obj ... Read now -
How To Exclude node_modules Directory When Running collectstatic Command in Django
If you use npm or yarn to install frontend packages inside your Django project, you may notice, that when you run python manage.py collectstatic command, it ends up collecting huge amounts of files. That's because by default, collectstatic grabs all content of static directories inside the project, including ... Read now -
Make Django Rest Framework and Axios Work Together Nicely
This is a solution to the problem I encountered while marrying Django Rest Framework powered API and Axios JS HTTP client: Axios issues GET requests with multi-value parameters in a bit different way than Django expects. When you create your API with Django Rest Framework, it expects multi-value GET parameters ... Read now -
Download AWS S3 Files using Python & Boto
<div class='alert-warning aler... -
How to use django-cache-memoize
Last week I released django-memoize-function which is a library for Django developers to more conveniently use caching in function calls. This is a quick blog post to demonstrate that with an example. The verbose traditional way to do it Suppose you have a view function that takes in a request and returns a HttpResponse. Within, it does some expensive calculation that you know could be cached. Something like this: No caching def blog_post(request, slug): post = BlogPost.objects.get(slug=slug) related_posts = BlogPost.objects.exclude( id=post.id ).filter( # BlogPost.keywords is an ArrayField keywords__overlap=post.keywords ).order_by('-publish_date') context = { 'post': post, 'related_posts': related_posts, } return render(request, 'blogpost.html', context) So far so good. Perhaps you know that lookup of related posts is slowish and can be cached for at least one hour. So you add this: Caching from django.core.cache import cache def blog_post(request, slug): post = BlogPost.objects.get(slug=slug) cache_key = b'related_posts:{}'.format(post.id) related_posts = cache.get(cache_key) if related_posts is None: # was not cached related_posts = BlogPost.objects.exclude( id=post.id ).filter( # BlogPost.keywords is an ArrayField keywords__overlap=post.keywords ).order_by('-publish_date') cache.set(cache_key, related_posts, 60 * 60) context = { 'post': post, 'related_posts': related_posts, } return render(request, 'blogpost.html', context) Great progress. But now you want that cache to immediate reset as soon as the blog posts change. … -
You have two jobs
Welcome to FictionalSoft! I hope your first week is going well? Great. As you start to find your feet, I want to make sure we have a shared understanding of what success looks like here. Apologies in advance if I’m telling you something you already know, but it’s important to be explicit about this early. You were hired to write code. Many developers make the mistake and think that their job stops there. -
Getting a MUD Roleplaying Scene going
Getting a MUD RP-scene goingThis article is a little different from the normal more technical Evennia-specific content of this blog. It was originally published as a light-hearted addition to the Imaginary Realities e-zine many years ago. While IR is still online it has since dozed off. So I'm reposting it here to bring it to a new audience. In roleplay-heavy MUDs (and in other categories of text-based roleplaying games), the concept of scenes become important. A scene in this concept is simply a situation big or small that involves you and your fellow players in interesting role play. A scene can be as simple as two players meeting in the street and exchanging a few words to the dramatic conclusion to a staff-driven quest. Whenever role player-interested players meet, a scene may happen.But sometimes scenes won’t come naturally. Sometimes your favourite game has only a few people online, or most of them are hovering in private areas. It’s time to go proactive. Below I offer some archetypes, tropes and ideas for how to get a random Scene started and people interested. The list is based on one I did for a RP-heavy MUD I played some time back.Some terms used … -
django-cache-memoize
Released a new package today: django-cache-memoize Docs On GitHub On PyPI It's actually quite simple; a Python memoize function that uses Django's cache plus the added trick that you can invalidate the cache my doing the same function call with the same parameters if you just add .invalidate to your function. The history of it is from my recent Mozilla work on Symbols. I originally copy and pasted the snippet out that in a blog post and today I extracted it out into its own project with tests, docs, CI and a setup.py. I'm still amazed how long it takes to make a package with all the "fluff" around it. A lot of the bits in here (like setup.py and pytest.ini etc) are copied from other nicely maintained Python packages. For example, I straight up copied the tox.ini from Jannis Leidel's python-dockerflow. The ratio of actual code writing (including tests!) is far overpowered by the package sit-ups. But I "complain with a pinch of salt" because a lot of time spent was writing documentation and that's equally as important as the code probably. -
PyCon.de keynote: Artificial intelligence,: differentiating hype and real value - Michael Feindt
(One of my summaries of a talk at the 2017 PyCon.de conference). He's a physics professor and started out with particle physics. The big experiments like in CERN. Big experiments that also generated Big Data. Think terabyte per second. This was long before the term "Big Data" was invented. Lots of data, you have to filter out the noise and find the actual signal. There's a fine balance there: if you are too careful, you'll never discover anything. If you're too enthousiastic, you can get wrong results. Wrong results are bad for your physics career, so the methods used were quite conservative. He had to fight to get more modern methods like neural networks accepted. What is intelligence? Two definitions: The ability to achieve complex goals. Ability to acquire and apply knowledge and skills. And artificial intelligence? All intelligence that is not biological. Biology, ok, what is life? "A process that retains its pcomplexity and replicates. DNA is abog 1.2GB. This is the physical life. Your brain is about 100TB. This is the "software". Cultural live. It accelerates through teaching, books, technology. Technological life? That will be when it can design its own hardware and software. He guesses robots/computers will … -
PyCon.de: Python on bare metal, micropython on the pyboard - Christine Spindler
(One of my summaries of a talk at the 2017 PyCon.de conference). (See also yesterday's talk) There's a lot of power and functionality in microcontrollers nowadays. But they are harder and harder to program. Wouldn't python be a great fit? It allows beginners to do things they couldn't do before. Micropython is a powerful and modern language with a large community, especially intended for very constrained/embedded systems. If you program for embedded systems, you really have to know the hardware. This is different from "regular" programming. Micropython started with a succesful kickstarter. In 2016, the BBC used it for 7 million school children. There was also a kickstarter for porting it to the super cheap ESP8266 chip. Fun facts: ESA (European space agency) is sponsoring development to make it even more reliable. They're planning to use it in satellites. It is certified for use in traffic management devices in the UK! There were some pyboards and people could play with it. Very nice is that you don't need an IDE: you can just connect to the board and type around on the python prompt. Photo explanation: some 1:87 scale figures on my model railway (under construction). -
PyCon.de: Observing your applications with Sentry and Prometheus - Patrick Mühlbauer
(One of my summaries of a talk at the 2017 PyCon.de conference). Monitoring your applications is important. You can fix problems before they happen. You can quickly pin-point them if they occur anyway. And you should get a good feel for the application through metrics. There are three 'pillars of observability': Logging. Records of individual events that happened. Metrics. Numbers describing a particular process or activity. CPU load, for instance. Tracing. Capture the lifetime of requests as they flow through the various components of a distributed system. (He won't talk about this). Error logging Logging in on a server and searching through logfiles is not much fun. Much better: sentry. It sends notifications for events (mail, slack, etc). It sends them only once. This is important. It aggregates events (statistics, regressions). There are lots of clients for multiple languages (python, javascript, etc.) and platforms (django, flask, angular). It is open source. There is also a software-as-a-service. He showed the user interface for an error. You see the line in your python code where the error occurred. Traceback. Statistics about browser types, server names, and so on. How often the error occurred already. When it occurred. It is easy to integrate. … -
PyCon.de: an admin's cornucopia, python is more than just better bash - Christian Theune
(One of my summaries of a talk at the 2017 PyCon.de conference). A "cornicopia" is a "horn of plenty". It keeps on giving. Pragmatism. I can quickly write some python fast for a task I need to do now. And it will be good enough. You can start right away: you don't need to start designing an architecture beforehand like you'd have to do in java. Often if you fix something quickly, you'll have to fix it a second time a day later. With python, you don't need to write your code twice. Perhaps 1.5 times. You can add tests, you can fix up the code. What do they use from python? Language features. Decorators, context managers, fstrings, meta programming. Python's standard library. You get a lot build-in. Releasing. zc.buildout, pip, virtualenv. Testing. pytest, flake8. Lots of external libraries. Some of these in detail. Context managers. Safely opening and closing files. They had trouble with some corner cases, so they wrote their own context manager that worked with a temporary file and guaranteed you cannot ever see a half-written file. Decorators. Awesome. For instance for automating lock files. Just a (self-written) @locked on a command line function. asyncio. They use … -
PyCon.de: the snake in the tar pit, complex systems with python - Stephan Erb
(One of my summaries of a talk at the 2017 PyCon.de conference). He started with an xkcd comic: Often it feels this way at the beginning of a project, later on it gets harder. You cannot just run import greenfield to get back to a green field again. Most engineering time is spend debugging. Most debugging time is spend looking for information. Most time spend looking for information is because the code/system is unfamiliar. Unfamiliar, unknown code: now we're talking about team size. You're probably debugging someone elses code. Or someone else is debugging your code. What can we do to understand such code? How can we spread the knowledge? You can do informal reasoning. Try to "run the code in your head". Code reviews. Pair programming. By getting better here, we create less bugs. Try to understand it by testing. Treat it as a black box. See what comes out. Add integration tests. Do load tests. Perhaps even chaos engineering. By getting better here we find more bugs. The first is better than the second way, right? Both get harder when it becomes more complex. Complexity destroys understanding. But I need understanding to have confidence. Keep in mind the … -
PyCon.de: graphql in the python world - Nafiul Islam
(One of my summaries of a talk at the 2017 PyCon.de conference). graphql is a query language for your API. You don't call the regular REST API and get the standard responses back, but you ask for exactly what you need. You only get the attributes you need. Graphiql is a graphical explorer for graphql. Github is actually using graphql for its v4 API. He did a demo. The real question to ask: why graphql over REST? There is a standard. No more fights over the right way to do REST. Development environment (graphiql). You get only what you want/need. Types. Lots of companies are using it already. What does python have to offer? graphene. Graphene uses these concepts: Types/objects. More or less serializers. Schema. Collection of objects and mutations. "Your API". Resolver. Query. What you can ask of the API. "You can search for users by username and by email". Mutations. Changes you allow to be made. "You can create a new user and you have to pass a username and email". He demoed it. It looked really comfortable and slick. Some small things: 2.0 is out (today!). The django integration is better than the sqlalchemy integration at the … -
PyCon.de: friday lightning talks
(One of my summaries of a talk at the 2017 PyCon.de conference). Parallel numpy with Bohrium - Dion Häfner He had to port a fortran codebase to numpy. Took a few months, but was quite doable. Just some number crunching, so you can do everything with numpy just fine. For production-running it had to run on parallel hardware. For that he used bohrium, a tool that works just like numpy, but with jit-compiled code. He showed some numbers: a lot faster. Cultural data processing with python - Oliver Götze Cultural data? Catalogs of book archives. Lots of different formats, often proprietary and/or unspecified and with missing data. And with lots of different fields. He wrote a "data preparation tool" so that they can clean up and transform the data to some generic format at the source. The power of git - Peer Wagner What do you think your repositories contain? Code? More! He read a book about "data forensics". git log is ok. But you can pass it arguments so that you can get much more info out of it. You can show which parts of your code are most often edited. You can also look which files are often …