Writeable Foreign keys in Django Rest Framework
Mon 09 December 2013 by guglielmoFiled under howto Tags python django django-rest-framework API
Django Rest Framework is a django-based framework to produce browseable APIs.
The documentation and the tutorial are pretty much clear, but implementing a writeable API for related fields can be tricky.
RelatedField is read-only. PrimaryKeyRelatedFields, HyperlinkedRelatedField, SlugRelatedField, can be used as writeable, but the related items must already exist. It seems they’re more appropriate to handle aggregation relations (i.e. User and Groups)
Nested relationships can be used to handle composition relations (i.e. a Place and its Acronyms). The quirk is that the get_identity() method must be re-written in the nested serializer, because the default one uses the id field to determine the identity of the items, which may not have, and which you surely don’t have when creating new items.
A working example with Places and Acronyms can be found here.
Given a Place instance with just one acronym, the JSON representation would be:
{ "_self": "http://localhost:8003/maps/places/roma-comune", "slug": "roma-comune", "name": "Roma", "acronyms": [ "RM" ] }
PUTting the following json payload to the resource:
{ "slug": "roma-comune", "name": "Roma", "acronyms": [ "RM", "ROM" ] }
or PATCHing the even shorter one:
{ "acronyms": [ "RM", "ROM" ] }
The final result would be that the ROM acronym would be added to the Place resource.
Using Mapnik inside a virtualenv on OSX
Mapnik is a tool to produce maps out of gis data.
Since installation in a virtualenv through pip install mapnik2 fails, due to compilation problems (boost library cannot be located correctly), the following workaround can be used.
Install Mapnik through macports:
sudo port install mapnik @2.2.0_0+python27
This ...
Using Tkinter inside a virtualenv
Tkinter is usually installed on OSX trhough macports as a global package:
sudo port install py27-tkinter
Within a virtualenv the package will not be imported, as global packages are not included by default:
>>> import Tkinter Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/local/Library ...read more
Fixing the “can’t start the AWT” bug for Tomcat6
After a Java upgrade on the OSX, Tomcat6 is not starting properly. The log shows a Java error:
2013-06-27 14:45:40.300 jsvc[57146:203] Apple AWT Java VM was loaded on first thread -- can't start AWT. Jun 27, 2013 2:45:40 PM org.apache.catalina.startup ...read more
How to configure bash completion in ubuntu
Working almost always in the terminal of a linux box, the autocompletion feature enhances your speed and comfort, a lot.
Under Ubuntu, autocompletion is installable, if not already installed, with:
apt-get install bash_completion
The ~/.bashrc config file should contain these lines:
if [ -f /etc/bash_completion ] && ! shopt -oq posix; then . /etc ...read more
How to install solr on tomcat7, over Ubuntu 12.04
Tomcat7
Ubuntu 12.04 comes with tomcat easily installable as a package (you need jdk):
apt-get install default-jdk tomcat7 tomcat7-admin tomcat7-user tomcat7-docs
Configure access to the web-admin in /etc/tomcat7/tomcat-users.xml:
<tomcat-users> <role rolename="manager-gui"/> <user username="manager" password="S3krEt" roles="manager-gui"/> </tomcat-users>
The tomcat management console is now ...
OpenSource in the UK
The UK government issued a beta version of the Service Design Manual, stating guidelines on how to design digital services according to the Digital by Default Service Standard.
Setting a digital service standard is part of the Governmental Digital Strategy, which aims at producing public digital services, “so good, that ...
read moreBureaucrats like Github
A post on Oreilly Radar informs us that Github is trending as the social-repository of choice for governments and public institutions who want to publish data, standards and source code.
Some remarkable examples:
- US - https://github.com/WhiteHouse
- NASA - https://github.com/nasa
- UK - https://github.com/alphagov
- Canada - https ...
BLOG TODO LIST
UPDATED by guglielmo Nov, 11th, 2012
- refine blog deploy flow (make/fab file) - OK
- make a theme - OK (sortof)
- add links
- disqus - OK
- google analytics - not OK