code: def write_models(model, data, field):
      
      written = []
  
      for hash_ in data:
          if field:
              if field not in hash_:
                  raise KeyError(("The \"%s\" field is required for all update "
                                  "operations") % field)
  
              model_kwargs = {field: hash_[field]}
              model_obj = model.objects.get(**model_kwargs)
  
              for to_set, val in hash_.items():
                  setattr(model_obj, to_set, val)
  
              model_obj.save()
  
          else:
              model_obj = model.objects.create(**hash_)
  
          written.append(model_obj)
  
      reset_sequence(model)
      return written
docs: :param model: a Django model class
      :param data: a list of hashes to build models from
      :param field: a field name to match models on, or None
      :returns: a list of models written
  
      Create or update models for each data hash.
  
      `field` is the field that is used to get the existing models out of
      the database to update them; otherwise, if ``field=None``, new models are
      created.
  
      Useful when registering custom tests with :func:`writes_models`.