<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _sort_by_unique_fields(model, model_objs, unique_fields):
 """ Sort a list of models by their unique fields. Sorting models in an upsert greatly reduces the chances of deadlock when doing concurrent upserts """

    unique_fields = [
        field for field in model._meta.fields
        if field.attname in unique_fields
    ]

    def sort_key(model_obj):
        return tuple(
            field.get_db_prep_save(getattr(model_obj, field.attname),
                                   connection)
            for field in unique_fields
        )
    return sorted(model_objs, key=sort_key)