  """
  import copy
  import inspect
  import traceback
  from collections import OrderedDict
  from collections.abc import Mapping
  
  from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
  from django.core.exceptions import ValidationError as DjangoValidationError
  from django.db import models
 
</s> remove                     validators.append(validator)
 </s> add                 field_names = tuple(source_map[f][0] for f in unique_together)
                validator = UniqueTogetherValidator(
                    queryset=parent_class._default_manager,
                    fields=field_names
                )
                validators.append(validator) </s> remove                 if field_names.issuperset(set(unique_together)):
                    validator = UniqueTogetherValidator(
                        queryset=parent_class._default_manager,
                        fields=unique_together
 </s> add                 # Skip if serializer does not map to all unique together sources
                if not set(source_map).issuperset(set(unique_together)):
                    continue

                for source in unique_together:
                    assert len(source_map[source]) == 1, (
                        "Unable to create `UniqueTogetherValidator` for "
                        "`{model}.{field}` as `{serializer}` has multiple "
                        "fields ({fields}) that map to this model field. "
                        "Either remove the extra fields, or override "
                        "`Meta.validators` with a `UniqueTogetherValidator` "
                        "using the desired field names."
                        .format(
                            model=self.Meta.model.__name__,
                            serializer=self.__class__.__name__,
                            field=source,
                            fields=', '.join(source_map[source]),
                        ) </s> remove         }
 </s> add         ))

        # Invert so we can find the serializer field names that correspond to
        # the model field names in the unique_together sets. This also allows
        # us to check that multiple fields don't map to the same source.
        source_map = defaultdict(list)
        for name, source in field_sources.items():
            source_map[source].append(name) </s> remove         field_names |= {
            field.source for field in self.fields.values()
 </s> add         field_sources.update(OrderedDict(
            (field.field_name, field.source) for field in self.fields.values() </s> remove         }
 </s> add         )
from collections import OrderedDict, defaultdict
  """
  import copy
  import inspect
  import traceback
from collections import OrderedDict, defaultdict
  from collections.abc import Mapping
  
  from django.core.exceptions import FieldDoesNotExist, ImproperlyConfigured
  from django.core.exceptions import ValidationError as DjangoValidationError
  from django.db import models
keep keep keep keep replace keep keep keep keep keep
 Fix ModelSerializer unique_together handling for field sources (#7143)

* Fix ModelSerializer unique_together field sources

Updates ModelSerializer to check for serializer fields that map to the
model field sources in the unique_together lists.

* Ensure field name ordering consistency 
rest_framework/serializers.py