def save(self, *args, **kwargs):
        for field, instance in self._foreignkeys_pre_save:
            instance = instance.__class__.remote.get_or_create_from_instance(instance)
            setattr(self, field, instance)
        self._foreignkeys_pre_save = []

        try:
            super(TwitterModel, self).save(*args, **kwargs)
        except Exception as e:
            import sys
            raise type(e), type(e)(e.message + ' while saving %s' % self.__dict__), sys.exc_info()[2]

        for field, instance in self._external_links_post_save:
            # set foreignkey to the main instance
            setattr(instance, field, self)
            instance.__class__.remote.get_or_create_from_instance(instance)
        self._external_links_post_save = []

        for field, instance in self._external_links_to_add:
            # if there is already connected instances, then continue, because it's hard to check for duplicates
            if getattr(self, field).count():
                continue
            getattr(self, field).add(instance)
        self._external_links_to_add = []
Save all related instances before or after current instance
dataset/ETHPy150Open ramusus/django-twitter-api/twitter_api/models.py/TwitterModel.save