python - Django 和 MongoDB 在 Django 模型上的默认 ID autoF

我有 Django 2,其中使用了 Rest Framework 和 Djongo。

可以创建帐户,但是当我尝试查看要更新的已创建帐户列表或对它们执行任何操作时,实际上我无法检索帐户列表并在 ID 字段上显示错误。

我认为这是 MongoDB 的问题,那么我该如何解决这个问题呢?

这是我遇到的错误,后面是我的帐户相关代码

MigrationError at /Accounts/account/ id Request Method: GET Request URL: http://localhost:8000/Accounts/account/ Django Version: 2.2.7 Exception Type: MigrationError Exception Value:
id Exception Location: C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\lib\site-packages\djongo\sql2mongo\query.py in _align_results, line 287 Python Executable: C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\python.exe Python Version: 3.8.0 Python Path:
['E:\Project\accountingPy', 'C:\Users\PC ' '001\AppData\Local\Programs\Python\Python38-32\python38.zip', 'C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\DLLs', 'C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32\lib', 'C:\Users\PC 001\AppData\Local\Programs\Python\Python38-32', 'C:\Users\PC 001\AppData\Roaming\Python\Python38\site-packages', 'C:\Users\PC ' '001\AppData\Local\Programs\Python\Python38-32\lib\site-packages'] Server time: Mon, 6 Apr 2020 15:00:38 +0000

存储在数据库中的记录

// 1
{
    "_id": ObjectId("5e8b3a0c10fc9f40cdca44be"),
    "label": "Mapping",
    "owner": "Nasser",
    "balance_currency": "USD",
    "balance": "23.00",
    "desc": "This the Cash account that manage by cash payments",
    "status": true,
    "created_at": ISODate("2020-04-06T14:17:48.838Z"),
    "updated_at": ISODate("2020-04-06T14:17:48.838Z")
}

账户模型

class Account(models.Model):
    class Meta:
        db_table = 'account'
    def __str__(self):
        return self.label

    label = models.CharField(max_length=20,unique=True)
    owner = models.CharField(max_length=20)
    balance = MoneyField(max_digits=14, decimal_places=2, 
                          default_currency='USD')
    desc = models.CharField(max_length=200, blank=True)
    status = models.BooleanField(default=1)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

账户序列化器

class AccountSerializer(serializers.ModelSerializer):
    class Meta:
        model = Account
        fields = ['label', 'owner', 'balance',
          'desc', 'status', 'created_at', 'updated_at']

网址

router = DefaultRouter()
router.register(r'', AccountViewSet, basename='account')
urlpatterns = router.urls

帐户 View 集

class AccountViewSet(viewsets.ViewSet):

    def list(self, request):
        account = Account.objects.all()
        serializer = AccountSerializer(account, many=True)
        return JsonResponse(serializer.data, safe=False)
      # return HttpResponse("List")

帐户来自

class AccountForm(ModelForm):
    class Meta:
        model = Account
        fields = ['label', 'owner', 'balance',
                  'desc', 'status']

基本解决方案 我将 id 字段 none autoIncrement 添加到模型中,问题已解决。

id = models.IntegerField(primary_key=True, unique=True)

但我认为这不是一个好的解决方案,因为 Id 不是自动递增的,每次我都需要检查最后一个 Id 是否有自动递增 ID 字段。 如何在 MongoDB 中创建自动增量字段?

最佳答案

我所做的是运行 python manage.py <app_name> zero至极删除所有迁移。然后我从迁移文件夹中手动删除迁移文件并运行 python manage.py makemigrations + python manage.py migrate .

帮我解决了。

关于python - Django 和 MongoDB 在 Django 模型上的默认 ID autoField 有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61062816/

相关文章:

azure-devops - Azure DevOps VsTest 任务失败且没有错误

python - 使用自定义容器从 AI Platform 训练作业访问 Google Secret

python - 如何处理 Python 中不同模块的相关对象之间的循环引用?

flutter - 实现 youtube 播放器时 Flutter App 出错

javascript - 如何在 Javascript 中获取选定的 pdf 文本?

python-3.x - 刚刚切换到 TensorFlow 2.1 并收到一些烦人的警告

amazon-web-services - Glue Crawler 无法识别时间戳

pyspark - 实例化时出错 'org.apache.spark.sql.hive.HiveEx

python - 使用 PyMC3 进行贝叶斯校准,Kennedy O'Hagan

django - HTTPConnectionPool(主机 ='0.0.0.0',端口=5000)