Month: March 2015

  • Save django model with dictionaries

    Beware when queryset filter update() because it will pass model validation. Here is some example save model with dictionaries : 12345678910for k, v in account.items():     setattr(instance, k, v) try:     instance.save() except Exception, e:     raise ValidationError(e) else:     updated += 1     # print account

  • Convert boolean True and False field value in Django Rest Framework

    Here is a quick snippet to convert boolean True / False in DRF Serializer: 123456is_finish = serializers.SerializerMethodField() def get_is_finish(self, obj):     if obj.is_finish:         return "Selesai"     return "Progress"

  • Modify object data by write Custom Renderer in Django Rest Framework

    When we need to modify object or results from DRF / Django Rest Framework, all we need just write a custom JSON Renderer like below : 1234567891011121314class JGridJSONRenderer(renderers.JSONRenderer):     def render(self, data, accepted_media_type=None, renderer_context=None):         new_rows = []         # Name the object list         […]

  • Python remove dictionary key if value is empty

    Here is a quick snippet if we want to remove dictionary key if value is empty: 1new_dict = {k: v for k, v in _data.items() if v if v is not ”}

  • Solve issue Fullcalendar count two day as one day

    This is not bug. When we try to add day in March with start 10-03-2015T10:00:00 and end 11-03-2015T02:00:00, then it will showing one day instead of two day. To solve this issue, we need to put nextDayThreshold: 1nextDayThreshold: "00:00" To make the next day threshold every 00:00

  • Counting number contain 14 on n-digit number in less than 1 second

    My fellow Adiyat in our company POLATIC bug me with this question “How to counting number contain 14 in 10 millions numbers less than 1 second?”. Then I thought, it’s easy, let give python a shot! 1print len([x for x in range(0, 10000000) if str(x).find(’14’) > -1]) With result “590040” it’s takes 9.6s based on […]

  • Django unit test formset

    When testing page that contains formset in Django using unit test, we may encounter : 1ValidationError: [u’ManagementForm data is missing or has been tampered with’] This is happend because we POST data without required FORMSET hidden value in form. To solve this issue, inspect element on your formset and insert hidden value in payload data. […]