I'm writing a website on Python, Django using MongoDB and Djongo (to connect Mongo with Django) and I want to be able to add and delete documents from the database using my website. But I have an error while doing it.
If I don't set primary key in my models then I can successfully add documents, but when try to delete have AssertionError (object can't be deleted because its id attribute is set to None), however when I check the database _ID
field is there created automatically. If I do set primary key by primary_key = True
in models.py I can successfully delete a document, but on insertion I got AssertionError (No exception message supplied).
Also, if primary key is not set, then I cannot access documents from admin panel, but can add them to the database (through admin panel); and if pk is set then from admin panel I can access, delete and edit, but cannot add a new document to the database.
This is my model:
class DevList(models.Model):
dev_num = models.CharField(max_length = 200 , primary_key = True)
dev_name = models.CharField(max_length = 200)
dev_descr = models.CharField(max_length = 200)
dev_type = models.CharField(max_length = 200)
If no pk is set, then I delete primary_key = True
line
My view is simple post form and I use d.save()
to save and d.delete()
to delete.
Actually, Django by default provides an
id
field with every model which is by default set asprimary key
.Now, when you can set other field as primary key by
primary_key=True
. But for smoother operation I will advice you to useunique=True
on other fields.