def check_and_update(a_model, unique_id_field='name', overwrite=False, **kwargs):
    """
    Check whether an object exists with the given name in the database. If the 
    object exists and "overwrite" is True, overwrite the object.  If "overwrite"
    is false, don't overwrite.  If the object doesn't exist, it is always created.

    @param a_model: A model class.
    @param unique_id_field: The field name that is unique.
    @keyword overwrite: Should the new object be overwritten?
    @returns: tuple - the object in the DB after any changes are made,
        whether the object had to be created, 
        whether the given attributes were consistent with what was in the database, 
        a message to return indicating any changes.
    """
    name = kwargs[unique_id_field]
    object_name = '%s %s' % (a_model.__name__, name)
    # Get the model if it exists
    try:
        id_args = { unique_id_field: name }
        current_object = a_model.objects.get(**id_args)
    # If it doesn't exist, just save it and return
    except ObjectDoesNotExist:
        new = a_model(**kwargs)
        new.save()
        return new, True, False, '%s created' % object_name
    # If it exists, track any changes and overwrite if requested
    different = []
    changed = False
    message = '%s matches database - no changes%s'
    for key in kwargs:
        current_value = current_object.__getattribute__(key)
        config_value = kwargs[key]
        if not (isinstance(current_value, types.StringTypes)) and not (isinstance(current_value, Model)):
            config_value = type(current_value)(config_value)
        if current_value != config_value:
            if overwrite:
                current_object.__setattr__(key, config_value)
                changed = True
                message = 'UPDATED %s; CHANGED attribute(s) "%s"'
            else:
                message = 'Didn\'t change %s; attribute(s) "%s" differ(s) from database configuration.\n\tWARNING: Sync your config file to your app configuration or use the -f switch with setup to force changes'
            different.append(key)
    if overwrite and changed:
        current_object.save()
    return current_object, False, len(different) > 0, (object_name, ', '.join(different)) % message
dataset/ETHPy150Open PublicMapping/DistrictBuilder/django/publicmapping/redistricting/config.py/check_and_update/swapped operands of `%`