docs: def bulk_upsert(
      queryset, model_objs, unique_fields, update_fields=None, return_upserts=False, return_upserts_distinct=False,
      sync=False, native=False
  ):
      """
      Performs a bulk update or insert on a list of model objects. Matches all objects in the queryset
      with the objs provided using the field values in unique_fields.
      If an existing object is matched, it is updated with the values from the provided objects. Objects
      that don't match anything are bulk created.
      A user can provide a list update_fields so that any changed values on those fields will be updated.
      However, if update_fields is not provided, this function reduces down to performing a bulk_create
      on any non extant objects.
  
      :type model_objs: list of dict
      :param model_objs: A list of dictionaries that have fields corresponding to the model in the manager.
  
      :type unique_fields: list of str
      :param unique_fields: A list of fields that are used to determine if an object in objs matches a model
              from the queryset.
  
      :type update_fields: list of str
      :param update_fields: A list of fields used from the objects in objs as fields when updating existing
              models. If None, this function will only perform a bulk create for model_objs that do not
              currently exist in the database.
  
      :type return_upserts_distinct: bool
      :param return_upserts_distinct: A flag specifying whether to return the upserted values as a list of distinct lists,
              one containing the updated models and the other containing the new models. If True, this performs an
              additional query to fetch any bulk created values.
  
      :type return_upserts: bool
      :param return_upserts: A flag specifying whether to return the upserted values. If True, this performs
              an additional query to fetch any bulk created values.
  
      :type sync: bool
      :param sync: A flag specifying whether a sync operation should be applied to the bulk_upsert. If this
              is True, all values in the queryset that were not updated will be deleted such that the
              entire list of model objects is synced to the queryset.
  
      :type native: bool
      :param native: A flag specifying whether to use postgres insert on conflict (upsert).
  
      :signals: Emits a post_bulk_operation when a bulk_update or a bulk_create occurs.
  
      Examples:
  
      .. code-block:: python
  
          # Start off with no objects in the database. Call a bulk_upsert on the TestModel, which includes
          # a char_field, int_field, and float_field
          bulk_upsert(TestModel.objects.all(), [
              TestModel(float_field=1.0, char_field='1', int_field=1),
              TestModel(float_field=2.0, char_field='2', int_field=2),
              TestModel(float_field=3.0, char_field='3', int_field=3),
          ], ['int_field'], ['char_field'])
  
          # All objects should have been created
          print(TestModel.objects.count())
          3
  
          # Now perform a bulk upsert on all the char_field values. Since the objects existed previously
          # (known by the int_field uniqueness constraint), the char fields should be updated
          bulk_upsert(TestModel.objects.all(), [
              TestModel(float_field=1.0, char_field='0', int_field=1),
              TestModel(float_field=2.0, char_field='0', int_field=2),
              TestModel(float_field=3.0, char_field='0', int_field=3),
          ], ['int_field'], ['char_field'])
  
          # No more new objects should have been created, and every char field should be 0
          print(TestModel.objects.count(), TestModel.objects.filter(char_field='-1').count())
          3, 3
  
          # Do the exact same operation, but this time add an additional object that is not already
          # stored. It will be created.
          bulk_upsert(TestModel.objects.all(), [
              TestModel(float_field=1.0, char_field='1', int_field=1),
              TestModel(float_field=2.0, char_field='2', int_field=2),
              TestModel(float_field=3.0, char_field='3', int_field=3),
              TestModel(float_field=4.0, char_field='4', int_field=4),
          ], ['int_field'], ['char_field'])
  
          # There should be one more object
          print(TestModel.objects.count())
          4
  
          # Note that one can also do the upsert on a queryset. Perform the same data upsert on a
          # filter for int_field=1. In this case, only one object has the ability to be updated.
          # All of the other objects will be created
          bulk_upsert(TestModel.objects.filter(int_field=1), [
              TestModel(float_field=1.0, char_field='1', int_field=1),
              TestModel(float_field=2.0, char_field='2', int_field=2),
              TestModel(float_field=3.0, char_field='3', int_field=3),
              TestModel(float_field=4.0, char_field='4', int_field=4),
          ], ['int_field'], ['char_field'])
  
          # There should be three more objects
          print(TestModel.objects.count())
          7
  
      """
      if not unique_fields:
          raise ValueError('Must provide unique_fields argument')
      update_fields = update_fields or []
  
      if native:
          if return_upserts_distinct:
              raise NotImplementedError('return upserts distinct not supported with native postgres upsert')
          return_value = Query().from_table(table=queryset.model).upsert(
              model_objs, unique_fields, update_fields, return_models=return_upserts or sync
          ) or []
          if sync:
              orig_ids = frozenset(queryset.values_list('pk', flat=True))
              queryset.filter(pk__in=orig_ids - frozenset([m.pk for m in return_value])).delete()
  
          post_bulk_operation.send(sender=queryset.model, model=queryset.model)
  
          return return_value
  
      # Create a look up table for all of the objects in the queryset keyed on the unique_fields
      extant_model_objs = {
          tuple(getattr(extant_model_obj, field) for field in unique_fields): extant_model_obj
          for extant_model_obj in queryset
      }
  
      # Find all of the objects to update and all of the objects to create
      model_objs_to_update, model_objs_to_create = _get_model_objs_to_update_and_create(
          model_objs, unique_fields, update_fields, extant_model_objs)
  
      # Find all objects in the queryset that will not be updated. These will be deleted if the sync option is
      # True
      if sync:
          model_objs_to_update_set = frozenset(model_objs_to_update)
          model_objs_to_delete = [
              model_obj.pk for model_obj in extant_model_objs.values() if model_obj not in model_objs_to_update_set
          ]
          if model_objs_to_delete:
              queryset.filter(pk__in=model_objs_to_delete).delete()
  
      # Apply bulk updates and creates
      if update_fields:
          bulk_update(queryset, model_objs_to_update, update_fields)
      queryset.bulk_create(model_objs_to_create)
  
      # Optionally return the bulk upserted values
      if return_upserts_distinct:
          # return a list of lists, the first being the updated models, the second being the newly created objects
          return _get_upserts_distinct(queryset, model_objs_to_update, model_objs_to_create, unique_fields)
      if return_upserts:
          return _get_upserts(queryset, model_objs_to_update, model_objs_to_create, unique_fields)