5👍
By default, if you have not specified the id
field for you model, Django will create AutoField:
id = models.AutoField(primary_key=True)
In your case you’ve created just IntegerField
. I suppose this is the point. In the database, it will still be incremented, but django won’t assign the id
value from database to the object, returned by get_or_create
By the way, in your question:
>>> b is c
False
b
– is a bool value. I think you were trying to do
>>> a is c
But also this is not a good idea. If you want to compare django model objects, use ==
, not is
:
>>> a == c
You will get False
anyway, as a
has its id
set to None
.
2👍
You have declared your id field as an IntegerField, so Django does not know it needs to autoincrement. You could define it as an AutoField, but actually there is no reason to declare it at all: Django will automatically define it for you if you leave it out.
Note this has nothing to do with get_or_create
: you would have exactly the same problem with a normal create/save.
- [Django]-What are some useful non-built-in Django tags?
- [Django]-DRF PrimaryRelatedField when write and NestedSerializer when read?
- [Django]-Unicode error when saving an object in django admin
- [Django]-Remove the decimal part of a floating-point number in django?