def save(self, model_name, value): update = False for i, model in enumerate(self.get_models(model_name)): if fetch_primary_key(model) == fetch_primary_key(value): self.get_models(model_name)[i] = value update = True if not update: if fetch_primary_key(value) == None: self.__dict__['ids'][model_name] += 1 value.id = self.__dict__['ids'][model_name] self.get_models(model_name).append(value) return update
def test_get_primary_key(): setup_test_environment() old_name = "django_app" from django.db import connection old_name = connection.creation.create_test_db(verbosity=1, autoclobber=True) stranger = Stranger(name="12") stranger.save() assert "12" == fetch_primary_key(stranger) connection.creation.destroy_test_db(old_name, 1) teardown_test_environment()