Looking at:
https://docs.djangoproject.com/en/3.1/ref/models/instances/#django.db.models.Model.save
For convenience, each model has an AutoField named id by default unless you explicitly specify primary_key=True on a field in your model. See the documentation for AutoField for more details.
and
https://docs.djangoproject.com/en/3.1/topics/db/models/
it seems clear that an object always has an id. I have a model:
class Currency(models.Model):
currency_name = models.CharField(max_length=100)
currency_value_in_dollars = models.FloatField()
currency_value_in_dollars_date = models.DateField()
def __str__(self):
return self.currency_name
that I've migrated as:
operations = [
migrations.CreateModel(
name='Currency',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('currency_name', models.CharField(max_length=100)),
('currency_value_in_dollars', models.FloatField()),
('currency_value_in_dollars_date', models.DateField()),
],
),
and when trying to add entries to the db like:
def update_coins_table():
if not do_greeting():
print("Gecko crypto board not reachable. Db setup")
return
crypto_coins_prices = cg.get_price(ids=coins_ids_str, vs_currencies='usd')
timezone_now = timezone.now()
for coin_key in crypto_coins_prices:
coin = Currency(coin_key, crypto_coins_prices[coin_key]['usd'], timezone_now)
coin.save()
the line:
coin = Currency(coin_key, crypto_coins_prices[coin_key]['usd'], timezone_now)
gives:
unable to get repr for <class 'manage_crypto_currency.models.Transaction'>
and
coin.save()
fails. If I replace the line in question with:
coin = Currency(1, coin_key, crypto_coins_prices[coin_key]['usd'], timezone_now)
it works. Shouldn't the id auto increment?
The last line always overwrites the previous and only one entry gets stored in the end.