1👍
✅
Django automatically sets a primary key field called id
unless you specify one yourself. So your Car
model will already have an id
field. It’s on the base model class that your model inherits.
In your example, you haven’t specified the defaults
argument of the get_or_create()
method, so Django will only attempt to retrieve an object with the values you’ve passed in via a get call: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#django.db.models.query.QuerySet.get_or_create
unique constraints can be specified on a field regardless if it’s an AutoField or not. See: https://docs.djangoproject.com/en/1.7/ref/models/fields/#unique for more specific information.
Source:stackexchange.com