How to save related fields (m2m) in Django's admin

Simple snippet how to save (update) related fields

class MovieAdmin(admin.ModelAdmin):
    list_display = ('id', 'title')
    save_on_top = True

    def save_model(self, request, obj, form, change):
        obj.imbd = get_imbd()

    def save_related(self, request, form, formsets, change):
        super(MovieAdmin, self).save_related(request, form, formsets, change)
        obj = form.instance
        if obj.comments.count() > 0:
            obj.comments_count = obj.comments.count()
comments powered by Disqus