def create_or_update(model, using=None, **kwargs):
 values = kwargs.pop('values', {})
 defaults = kwargs.pop('defaults', {})
 if (not using):
  using = router.db_for_write(model)
 objects = model.objects.using(using)
 affected = objects.filter(**kwargs).update(**values)
 if affected:
  return (affected, False)
 create_kwargs = kwargs.copy()
 inst = objects.model()
 for (k, v) in itertools.chain(six.iteritems(values), six.iteritems(defaults)):
  if (not isinstance(v, Model)):
   k = model._meta.get_field(k).attname
  if isinstance(v, ExpressionNode):
   create_kwargs[k] = resolve_expression_node(inst, v)
  else:
   create_kwargs[k] = v
 try:
  with transaction.atomic(using=using):
   return (objects.create(**create_kwargs), True)
 except IntegrityError:
  affected = objects.filter(**kwargs).update(**values)
 return (affected, False)

def create_or_update ( model , using = None , ** kwargs ) : values = kwargs . pop ( 'values' , { } ) defaults = kwargs . pop ( 'defaults' , { } ) if ( not using ) : using = router . db_for_write ( model ) objects = model . objects . using ( using ) affected = objects . filter ( ** kwargs ) . update ( ** values ) if affected : return ( affected , False ) create_kwargs = kwargs . copy ( ) inst = objects . model ( ) for ( k , v ) in itertools . chain ( six . iteritems ( values ) , six . iteritems ( defaults ) ) : if ( not isinstance ( v , Model ) ) : k = model . _meta . get_field ( k ) . attname if isinstance ( v , ExpressionNode ) : create_kwargs [ k ] = resolve_expression_node ( inst , v ) else : create_kwargs [ k ] = v try : with transaction . atomic ( using = using ) : return ( objects . create ( ** create_kwargs ) , True ) except IntegrityError : affected = objects . filter ( ** kwargs ) . update ( ** values ) return ( affected , False )
similar to get_or_create .