DetailView on Django REST – multiple methods

Long time since I wrote something on Django, but today it surprised me again with its flexibility. Let me put it down here. Remember the traditional DetailView ?

Example from:

from django.views.generic.detail import DetailView
from articles.models import Article<img src="blob:" alt="Selection_407.png" class="alignnone size-full wp-image-media-2"/>

class ArticleDetailView(DetailView):

    model = Article

    def get_object(self): 
            return Article.objects.get(pk=self.kwargs.get('pk'))

Setting this thing up in Angular to retrieve details of an Article, I used to follow something similar:

from articles.models import Article
from django.http import Http404

class ArticleDetailView(generics.RetrieveAPIView):

    model = Article
    serializer_class = ArticleDetailsSerializer
    queryset = Article.objects.all() 

    def get_object(self): 
            return Article.objects.get(pk=self.kwargs.get('pk'))
        expect ValueError:
            return Http404

I was looking through the documentation agian , and I see that the entire thing can be done in like 3 lines. Take a look:

from articles.models import Article

class ArticleDetailView(generics.RetrieveAPIView):

    model = Article
    serializer_class = ArticleDetailsSerializer
    queryset = Article.objects.all() 

Note: This should work only if you have Article primary key received on as pk. ie, something like this in your

from django.conf.urls import url

from article.views import ArticleDetailView

urlpatterns = [
    url(r'^article/(?P<pk>[\w]+)/$', ArticleDetailView.as_view(), name='article-list'),

In case you do not want to send in the primary key, you can provide the custom slug name using the attribute lookup_fields in your view definition. Read more here:

An example request to the URL would be Django for life, Enjoy humans.

WikiToLearn India conf 2017, a late recollection

Firstly, I still can’t believe that it took almost 4 weeks for me to come up with a write-up on a conference – looks like I’m slipping. Well, it’s the exam month, and lecture slides are as boring as usual.

Food is <3
Food is ❤

So this was my first ever conference under the sponsorship of KDE, and of course, when you are invited cross-continent – you have the responsibility to make every moment count. I personally do not know how effective I was, but I can tell that this one was one of the best and most active University level conference I had been in – like the people just wanted more and more – which was Wow.

Since this post is coming late, I will have to skip few details here, which other speakers have already talked about in their own blogs. So for me, it started on Jan 16, 2017, morning – and let me tell you that the journey all the way to Jaipur was not the best I had to go through. Like if I compare the route I took with the shortest path – bah, but it was enthralling and peaceful.

Reaching back to India
This was indeed special, as it was like 4 months since I left the country, and upon reaching back I had couple of anxieties too – like things have changed. So the WTL organizing team was there to pick me up from the airport, and yay – we reached our accommodation in the Uni. I haven’t slept for like 24 hours or more, and just went to sleep. I woke up with a call that Ricardo Iaconelli, the founder of WTL is sharing the room with me. He had just flown all the way from Milan, and yeah – we had actually met during Wikimania 2016 back at Esino Lario, Italy. Both of us expressed how cold we felt even though coming from still colder countries (me here in DE) – probably we were not expecting a cold -6 deg.

0th day
Mostly spend the time roaming around with Ricardo and also trying to get connected to some form of internet to complete my slides for the talk in two days. I was super happy to have some fine Indian food at last, and yay – I could see from the face Ricardo too was. From my experience with Italians, it’s difficult to impress them with food, but I think the butter paneer got him.

Met my long lost friend R Harish that night, and wait – he didn’t notice the damage 4 months in Berlin had done to my hair until I took my cap off, and he just backed off for a moment. I need to quote it here that I forgot that cap then and there like it never reached back with me to Berlin. damn.


Meeting long lost friends, after long time.
Meeting long lost friends, after long time.

That night we had an online webinar with some serious guy who was bragging something how he invented something, and how he couldn’t license it properly to make it proprietary. I am not naming the things here as this bloke is famous for suing people who stand up and say he is a jerk. Ricardo and myself were deeply moved by this talk, and we had discussions about how Open things should be, and how dangerous tendencies like this bloke can be. I guess this affected most of the excellent keynote he gave the next day too.

I cannot stop the first day without mentioning a machine, Sagar Agarwall, and I wish him better health – nothing more. He’s got all the rest, as I see it from this updates. Get some sleep, man, please.

That night was pretty cold, and let me tell you the worst cold I had in like the past 5 months or so (Ricardo too as freezing).

First day, talks, keynotes and afternoon bash
Things started up, and a modest crowd turned up – yeah – it was more than what you usually expect on hackathons and stuff – probably because it was happening as part of the LNMIIT tech fest PLINTH. By the way, I have to mention Srijan, Haritha and Chaithanya – my juniors during Bachelors and was speakers and organizing as well. I was happy to see them and nice to talk Malayalam – finally.

As a last bench student, I barely listen to lectures – but I tried my best to listen wholeheartedly to all the wonderful presentations which followed, and with the good food and an amazing keynote by Ricardo, the first day was almost coming to an end.

The evening was the best thing I remember about the first day when Sagar asked the participants to start mingling with the mentors around. I had a small but nice team around me, and they had pretty nice questions and stuff about GSoC, contributions, etc. That’s my cake, and I hope I could entertain them with the stuff I had. This went on for like 45 minutes or so – and still continued, people just wanted to talk and gather information – how cool.

Evening sessions were like ...
Evening sessions were like (well)!

During the night, we had a small live hands-on hackathon session – which was again pretty nice. I think the point of the 2-hour hackathon was to make people excited about programming languages, some use cases for using a version control etc. I again got a small pack of students near me, and we really did some serious code – yeah really. With their support and asking them to think about the logic involved, we coded down a python script that would download all images from a given Wikipedia page, and store it in a folder according to the original image names – how cool is that. Personally, I was enjoying myself, and I could see from their face that they were too – don’t people entertain python jokes these days?

That night went on and on, and we had more people coming in asking new questions, and we were solving it one after the other – what a crowd.

That night was cold again, but we managed to get a heater – thanks to one of the WikiToLearn organizers (I forgot his name), but you people saved us. I have to mention the endless fun the speakers had in between, and most of my time was dedicated interacting with them and the participants – which was my original plan, of course.

Second day, my talk, photographs, evening FAQ and party
I woke up early to make sure things were set for my presentation, and with the little help from here and there, I was ready with my slides here. The presentation started with a delay of like 15 mins or so, but the crowd was fresh – and it went well (I guess)? I have to still get the video which the team was recording to analyze how it went, though. I tried my best to avoid text from the slides, adding in more graphical and logical order for important topics – like contributing to an Open Source project, which I think makes it more clear.

Serious talks going on - yeah - thats me
Serious talks going on – yeah – thats me

I don’t know what worked, but after the presentation, we still had loads of students coming in and asking in detail about things to be done, how to contribute etc – which was energizing. By the end of the day, I literally lost voice, but we still planned one more short FAQ session where we asked people to write a question if they have in mind to ask to the speaker panel. That really went well, and we did have some really funny questions and answers. I could see from their faces that all the speakers were having fun – yeah that was one of my best social days ever.

Closing in, leaving town
It was time to wind things up on 19th night – and surprisingly we had a pro show running on the stage, and we were invited! Yay! Ricardo and me sneaked out to the back where we met people who were previously in the conference and they were delighted to see us \m/ We partied together, and at some point they lifted both of us up on their shoulders, and that was the best thing I did in Jaipur that day (or was it people making me to do).

I left later to the airport with Ms. Srivastava – one among our amazing speaker pack. Delhi was foggy, but personally, I felt that I should’ve stayed a couple of days more at Jaipur.

Things that worked out
I don’t usually compare, but I think things really worked out this time with the WTL conference as the speaker panel was pretty nice – yeah it was – and no I’m not talking about me. Other things that worked out was maybe the excellent hand’s own session, and Sagar’s push to people to interact with the speakers – which made them too close with us. The keynote and the ‘guys’ talk on the 0th day too triggered things which made people think about why they should be passionate about Open Source in the first place. How nice?

Things that didn’t worked out
* Heater in our room (:P)
* couldn’t find anything more – it went perfectly as per planned, and more or less the speakers were so comfortable with each other that we were happy to make ourselves home.

I need to end this thing now, but I can assure you there are tons of things which I really missed in this post. The entire thing was written up fast, as I still have exams coming up and my scheduler is looking angrily at me. Thank you, KDE, Sagar, Srijan and the team for such a nice event – and to the amazing participants out there – much love. The speaker’s team – you people were <3.

The event website:

The entire team
The entire team

Surprise from Python on new years eve, 2016

Alright, so another usual night and I was trying to figure out a strange behavior with a Python class I was working. Looks like I had OOP lectures only in C++, and things are little bit different here. So here you go!

I had this strange class method here described as:

class BenchmarkResults(object):

    def _generate_db_entries(cls, tests, skip_aggregated=False, parent=None, test_table = []):
         # This function recursively calls _generate_db_entries with
         # test_table param set to a list 

and this was processing in a JSON file from one of my POST API and doing some crazy stuff. The API calls this class like

class BotReportView(APIView):
    def post(self, request, format=None):
        test_data_results = BenchmarkResults(json.load(self.request.FILES.get('test_data')))
        results_table = test_data_results._generate_db_entries(test_data_results)

Now the funny part. I found that on subsequent calls to the API, the test_data_results kept on multiplying 😮 like the data from the previous POST also got appended to the current JSON which-should-be-processed. Thinking it was a problem with the client, I tried switching to curl, but with little luck.

The problem
Finally pycharm got me figuring out what went wrong, as it showed a warning on the test_table = [] something like:
The default method argument is mutable. Bah, taking at look at I see that when an empty list [] is kept as a default formal argument for a function, the list is not killed in between, and keeps on growing on subsequent calls – surprise :\

Quoting, when we have something like:

def status(options=[]):
    return options

print status()
print status()
print status()

will print something like

['new_option', 'new_option']
['new_option', 'new_option', 'new_option']

Tough luck finding it out, but it took couple of hours 😀 I got my code easily fixed by not allowing [] to stay as a default param, but separately passing an empty list to the function during the call. Like

class BenchmarkResults(object):

    def _generate_db_entries_helper(cls, tests, skip_aggregated=False, parent=None):
        test_table = []
        self._generate_db_entries(cls, tests, test_tables skip_aggregated=False, parent=None):

    def _generate_db_entries(cls, tests, test_table, skip_aggregated=False, parent=None):
         # This function recursively calls _generate_db_entries with test_table param set to a list 

Yay. that was it, and latter called in the helper function, and things started working. You can see the change here, and finally, Happy New year 2017! Make every commit count!

[Angular JS] Setting a combobox based on selection of another one

This might be my first post which touch Angular JS, and wanted to let you guys know that I too joined the backend to frontend club! Currently Angular JS is powering with Django REST at the backend – and I think its super fast, easy to develop and allows for a cleam responsive UI – WOW is the word.

You have a data model, which looks something like:

bots = [{ 'bot1' : {'platform': "Linux", id:1}}, { 'bot2' : {'platform': "Windows", id:2}}];
platforms = [{ 'name' :'Linux', 'id' :1},{ 'name' :'Windows', 'id' :2}, { 'name' :'MacOSuX', 'id' :3}];     

You have a select box bots and another select box Platforms and you need the following logic to happen:

  1. When no bot is selected,the Platforms combo should show All all the platform options
  2. When a bot is selected, it should force the Platform combo to show the platform property of bot selected, and should disable them to make no further changes.

You will need the following Angular script to make this work. Add this to your scripts.js or wherever place and include it in your html file.

app.controller('AppController', function($scope) {
    $scope.bots = [{ 'name': 'bot1', 'platform': "Linux", id:1}, { 'name': 'bot2', 'platform': "Windows", id:2}];
    $scope.platforms = [{ 'name' :'Linux', 'id' :1},{ 'name' :'Windows', 'id' :2}, { 'name' :'MacOSuX', 'id' :3}];     

see that you can even add this directly to the html file with the tag. Now you can add the following to the HTML file so that the data gets correctly rendered to the browser.

<div class="form-group">
  <label for="bot">Bot</label>
  <select name="bot" class="form-control" ng-model="selectedBot"
	  ng-options=" for bot in bots">
      <option value="">--All bots--</option>
<div class="form-group">
  <label for="platform">Platform</label>
  <select name="platform" class="form-control" ng-model="selectedPlatform" ng-disabled="selectedBot">
      <option value="">--All platforms--</option>
      <option ng-repeat="x in platforms" value="{{ }}">{{ }}</option>
      <option value="{{ selectedBot.platform }}" ng-selected="selectedBot" ng-show="selectedBot">
	  {{ selectedBot.platform }}</option>

How it works?

  • The {{ }} makes sure that all the platforms are shown initially making sure our condition (1) above.
  • The {{ selectedBot.platform }} makes sure that when the bot select is clicked, this option value is set for the combo box – adding to (2) above.
  • The ng-selected="selectedBot" ng-show="selectedBot" makes sure that the combo gets disabled when someone selects in some value for bots contributing to (2) above
  • You can see the dropdowns live in action at and do take a look and comment if something seems weird.

    Update: It seems like there was a problem with this approach as the selectedPlatform is not getting set correctly, even though the combo gets set. You can fix this by
    adding in the following action on ng-change on the select combo

    <select name="bot" class="form-control" ng-model="selectedBot"
          ng-options=" for bot in bots" ng-change="updateOtherCombos()">
      <option value="">--All bots--</option>

    and now defining the function updateOtherCombos() to your controllers.js

    app.controller('AppController', function($scope) {
        $scope.bots = [{ 'name': 'bot1', 'platform': "Linux", id:1}, { 'name': 'bot2', 'platform': "Windows", id:2}];
        $scope.platforms = [{ 'name' :'Linux', 'id' :1},{ 'name' :'Windows', 'id' :2}, { 'name' :'MacOSuX', 'id' :3}];     
        $scope.updateOtherCombos = function () {
          if ( $scope.selectedBot ) {
    	  $scope.selectedPlatform = $scope.selectedBot.platform;
    	  $scope.selectedCPU = $scope.selectedBot.cpu;
    	  $scope.selectedGPU = $scope.selectedBot.gpu;