          # including those.
          field_names = {
              field.source for field in self._writable_fields
              if (field.source != '*') and ('.' not in field.source)
          }
  
          # Special Case: Add read_only fields with defaults.
          field_names |= {
              field.source for field in self.fields.values()
              if (field.read_only) and (field.default != empty) and (field.source != '*') and ('.' not in field.source)
 
</s> remove         field_names = {
            field.source for field in self._writable_fields
 </s> add         field_sources = OrderedDict(
            (field.field_name, field.source) for field in self._writable_fields </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         ))

        # 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                 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                     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)
        )
          # including those.
          field_names = {
              field.source for field in self._writable_fields
              if (field.source != '*') and ('.' not in field.source)
        )
  
          # Special Case: Add read_only fields with defaults.
          field_names |= {
              field.source for field in self.fields.values()
              if (field.read_only) and (field.default != empty) and (field.source != '*') and ('.' not in field.source)
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