ID 为“None”的 djongo 公司不存在。也许它被删除了?

djongo Company with ID “None” doesn’t exist. Perhaps it was deleted?

我在类似的问题中找不到解决方案。

使用 mongosh,公司对象确实存在,但在管理员中,它们显示为对象(None),因此由于错误“公司 ID”None”不存在,可能是被删除了?”。我猜这是关于“id”检测,但我自己无法修复。问题:如何修复代码以使 Company 对象正确显示,而不是 None.

myproject> db.companies_company.find()
[
  { _id: ObjectId("6145dd9a8bc9a685b2ae2375"), name: 'company1' },
  { _id: ObjectId("6145ddaa8bc9a685b2ae2377"), name: 'company2' }
]

models.py:

from django.db import models

# Create your models here.
class Company(models.Model):
    name = models.CharField(max_length=100, blank=False, null=False, unique=True)

admin.py:

from django.contrib import admin

# Register your models here.
from .models import Company

@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    pass

_id 字段的手动设置解决了这个问题。

from djongo import models

# Create your models here.
class Company(models.Model):
    _id = models.ObjectIdField()
    name = models.CharField(max_length=100, blank=False, null=False, unique=True)