Categories
Django Python

Django REST Framework: Displaying full URL for ImageField or FileField

If you have any ImageField or FileField in your model, you can easily display the full URL (including the hostname/domain) for the file/image. Django REST Framework’s model serializers would do it for you. However, to get the hostname/domain name, the serializer needs a “request context” so it can infer the necessary parts and build a full url.

So if you’re manually invoking a serializer, please pass a request context like this:

If you use ModelViewSet, DRF will automatically pass the request context while initializing the serializer. So in that case you don’t need to do anything. You need to pass the context only when you’re manually creating a serializer instance.

Categories
Django Python

Django REST Framework: Dynamic Fields in Serializers

Here’s the use case, I need to add some extra fields to my serializer depending on a request. For example, if the query string contains “extra=true”, then add the extra fields. Luckily, serializers get a “context” argument when they are initialized. We can use this to customize our serializers as needed.

The fields defined on a serializer are available in a instance variable named “fields”. So we can add/delete/edit fields from it. Let’s see an example:

Please note, here we have used another serializer (UserLocationSerializer) from inside our main serializer. The second one is being initialized by us. So it would not get the context. If we need to do something down there as well, then we need to pass it ourselves.

Now the second serializer will get the request too and we can use the same way to customize it!

Categories
Python

Docker Workflow of Iron.io: Python Workers

If you’re planning to use Iron.io with Python, their new docker workflow is pretty cool. In this blog post, I shall walk you through the setup.

Setup Docker

The first step would be to install Docker. Here’s a pretty nice installation guide from Digital Ocean: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-getting-started 🙂

If you’re not using Ubuntu (eg. Windows or OS X), you might consider using Ubuntu in a virtualbox. I usually use Vagrant to do that.

If you’re using another Linux distro, please go through the official docker docs, I’m sure it’s quite easy 🙂

Once the setup is complete, run the following command to confirm that Docker has successfully installed and currently running:

You would see something like this:

Installing PIP Dependencies

Now it’s the time to install the dependencies for our projects. Change directory to our project root and use the requirements.txt file to install the packages. Please note we shall install them into a separate directory than the usual system path.

Here we have asked docker to run a new container based on the “iron/images:python-2.7” image. Inside the container we run the pip command. The “-t” flag would install packages in a separate target directory.

Since this is the first time we’re using this image, docker will need to pull it first. Then it will continue to install the dependencies. Once it’s done, feel free to examine the packages directory.

Locally Running Workers

The command is similar, except we have the packages installed in “packages” directory. So we need to set the PYTHONPATH env variable.

That would work 🙂

Bundling workers

To bundle the workers, we need to first zip everything:

You must remember to zip everything in, including the packages directory.

Now, we can start using the iron cli. In case you don’t have it installed, it’s easy:

Now let’s upload the zip file:

I think the command is pretty self explanatory. However, for the env argument, you need a file named “iron.json” which would contain the project id and tokens for your different environments. Here’s a sample:

Also note, we’re using PYTHONPATH=”packages” before invoking Python.

If everything goes right, your worker will be packaged and uploaded. Try running it to see how it goes.