GG.pl REST API: python-ggapi
Yesterday, we’ve released simple implementation of GG.pl API. GG.pl is Facebook-like site based on most popular polish IM - Gadu-Gadu.
python-ggapi is a client library that allows you to send notifications to users, get their data from public directory, and post to dashboard.
More about GG.pl and it’s API: http://dev.gg.pl (in polish).
Library is licensed under OSI BSD license. For manual and download check github: https://github.com/elcodo/python-ggapi
Wczoraj wydaliśmy pierwszą wersję modułu Python obsługującego API serwisu GG.pl.
Pozwala on na pobieranie danych z katalogu publicznego, wysyłanie notyfikacji do użytkowników, umieszczanie wiadomości na pulpicie użytkownika.
Więcej o API GG.pl: http://dev.gg.pl
Dokumentację i kod źródłowy (licencja BSD) można znaleźć na github: https://github.com/elcodo/python-ggapi
Django: How to see if model instance was changed after save (or how to keep model instance state)
There are situations when you want to know when model instance was changed. E.g. slugs generation when you change title (or maybe this example is discussable when comes to SEO?).
SEO matters but there are some real-life situations when you need this - let’s look at following example:
- you’re using django-mptt or some other method to store your document tree,
- URL to your document is generated using all ancestor names like: “Document” with parent “Data” has an URL like this: /data/document/
- you want to rebuild all children URLs when root or any parent document title/slug changes.
Nevertheless, there is some very simple solution to detect model state.
In your models.py, add new property like that:
class ExampleModel(models.Model):
# <<model fields goes here>>
before_save = None
Now, it’s time for signals - we will use pre_save and post_save. First one for getting before save data, second one to recognize it and do some action.
It is easy to detect field value change, e.g. title change generates new slug. See following example:
def object_pre_save(instance, **kwargs):
"""Store object state before save"""
try:
instance.before_save = ExampleModel.objects.get(pk=instance.pk)
except Transaction.DoesNotExist:
instance.before_save = None
def object_post_save(sender, instance, created, **kwargs):
if instance.before_save.title != instance.title:
# <<generate new slug method>>
instance.save()
pre_save.connect(object_pre_save, sender=ExampleModel)
post_save.connect(object_post_save, sender=ExampleModel)
Of course, you can do more complex lookups for change since there is complete “before-save” instance avaible at instance.before_save.
Okay, it is using extra query but if you do it right - e.g. in admin it shouldn’t kill your service. Maybe you have better solution for checking if data has changed? Let us know!

