[Answered ]-Save all data in database with one query in Django

1👍

Yes! Take a look at bulk_create() documentation. https://docs.djangoproject.com/en/4.0/ref/models/querysets/#bulk-create

If you have a db that supports ignore_conflicts parameter (all do, except Oracle), you can do this:

new_currencies = []
for currencie in currencies:
    name = currencie['name']
    symbol = currencie['symbol']
    active = (False, True)[symbol.endswith('USDT')]
    oms = 'kucoin'
    new_currencies.append(Instrument(name=name, symbol=symbol,
                        active=active, oms=oms))
Instrument.objects.bulk_create(new_currencies, ignore_conflicts=True)

1-liner:

Instrument.objects.bulk_create(
[
    Instrument(
        name=currencie['name'], symbol=currencie['symbol'], 
        active=currencie['symbol'].endswith('USDT'), oms='kucoin'
    )
    for currencie in currencies
], 
ignore_conflicts=True
)

Leave a comment