I have a Django backend project connected to a Mongo database.
I want to manually and personally define the field ID inside this class to be stored in the database so that Django and Mongo can communicate with each other so that I can edit or delete through the Django admin panel.
from djongo import models
class Category2(models.Model):
# id =
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
price = models.PositiveIntegerField()
class Meta:
ordering = ('name',)
def __str__(self):
return self.name
Unfortunately, I have no idea how to do it
2
Answers
NOTE – After making changes to your model, you may need to perform migrations (
python manage.py makemigrations
andpython manage.py migrate
)By default, Django use a hidden primary key field, an integer, that is autoincremented. If you want to use another field type, for example
CharField()
, you need to add theprimary_key=True
to thatCharField
. Example:Source: Automatic primary key fields