def METHOD_NAME(cls, relation, type, avatar=None, filename=None, color=None) -> Self:
    if avatar:
        # Create an instance of the current class so we can
        # access where new files should be stored.
        dummy = cls()
        file_class = dummy.file_class()
        with atomic_transaction(using=router.db_for_write(file_class)):
            photo = file_class.objects.create(name=filename, type=cls.FILE_TYPE)
            # XXX: Avatar may come in as a string instance in python2
            # if it's not wrapped in BytesIO.
            if isinstance(avatar, str):
                avatar = BytesIO(force_bytes(avatar))
            # XXX: Avatar processing may adjust file position; reset before saving.
            avatar.seek(0)
            photo.putfile(avatar)
    else:
        photo = None
    with atomic_transaction(
        using=router.db_for_write(cls),
    ):
        if relation.get("sentry_app") and color is not None:
            instance, created = cls.objects.get_or_create(**relation, color=color)
        else:
            instance, created = cls.objects.get_or_create(**relation)
        file = instance.get_file()
        if file and photo:
            file.delete()
        if photo:
            if instance.file_fk() != instance.file_write_fk():
                setattr(instance, instance.file_fk(), None)
            setattr(instance, instance.file_write_fk(), photo.id)
            instance.ident = uuid4().hex
        instance.avatar_type = [i for i, n in cls.AVATAR_TYPES if n == type][0]
        instance.save()
    if photo and not created:
        instance.clear_cached_photos()
    return instance