Django n关于哪些行不能插入的信息?在我个人看来,这确实是最好的,因为它允许您捕获错误,而这些错误最终会作为“忽略冲突”的一部分被忽略。 LOCK TABLE realtable IN EXCLUSIVE MODE; INSERT INTO realtable

Django n关于哪些行不能插入的信息?在我个人看来,这确实是最好的,因为它允许您捕获错误,而这些错误最终会作为“忽略冲突”的一部分被忽略。 LOCK TABLE realtable IN EXCLUSIVE MODE; INSERT INTO realtable ,django,postgresql,bulk-load,Django,Postgresql,Bulk Load,n关于哪些行不能插入的信息?在我个人看来,这确实是最好的,因为它允许您捕获错误,而这些错误最终会作为“忽略冲突”的一部分被忽略。 LOCK TABLE realtable IN EXCLUSIVE MODE; INSERT INTO realtable SELECT * FROM temptable WHERE NOT EXISTS ( SELECT 1 FROM realtable WHERE temptable.id = realtable.id ); def psql_cop


n关于哪些行不能插入的信息?在我个人看来,这确实是最好的,因为它允许您捕获错误,而这些错误最终会作为“忽略冲突”的一部分被忽略。
LOCK TABLE realtable IN EXCLUSIVE MODE;

INSERT INTO realtable 
SELECT * FROM temptable WHERE NOT EXISTS (
    SELECT 1 FROM realtable WHERE temptable.id = realtable.id
);
def psql_copy(records):
    count = len(records)
    if count < 1:
        return True
    try:
        pg.copy_bin_values(records)
        return True
    except IntegrityError:
        if count == 1:
            # found culprit!
            msg = "Integrity error copying record:\n%r"
            logger.error(msg % records[0], exc_info=True)
            return False
    finally:
        connection.commit()

    # There was an integrity error but we had more than one record.
    # Divide and conquer.
    mid = count / 2
    return psql_copy(records[:mid]) and psql_copy(records[mid:])
    # or just return False
objs = [(Event), (Event), (Event)...]

try:
    Event.objects.bulk_create(objs)

except IntegrityError:
    for obj in objs:
        try:
            obj.save()
        except IntegrityError:
            continue
from querybuilder.query import Query
q = Query().from_table(YourModel)
# replace with your real objects
rows = [YourModel() for i in range(10)] 
q.upsert(rows, ['unique_fld1', 'unique_fld2'], ['fld1_to_update', 'fld2_to_update'])
create_list = []
create_list_check = []
for brand in brands:
    if (owner.id, brand) not in create_list_check:
        create_list_check.append((owner.id, brand))
        create_list.append(ProductBrand(owner=owner, name=brand))

if create_list:
    ProductBrand.objects.bulk_create(create_list)