我正在处理一个关于Djongo的项目,其中包含一个抽象模型和一个主要模型。当我尝试插入一个值时,它可以无错误地被插入。但是当我尝试检索数据时,会收到“无法实例化抽象模型”的错误提示。
以下是我的模型:
class Exam_questions(models.Model):
question=models.CharField(max_length=200,null=True)
options=ArrayField(models.CharField(max_length=50))
answer=models.CharField(max_length=50,null=True)
types=models.CharField(max_length=50,null=True)
class Meta:
abstract=True
class ExamDetails(models.Model):
_id=ObjectIdField()
exam_id=models.IntegerField(null=False,default=0)
questions=models.EmbeddedField(model_container=Exam_questions)
objects = models.DjongoManager()
这是我的查询代码:
def exams_questions(request,exam_id):
get_exams=ExamDetails.objects.filter(exam_id=3)
print(get_exams)
return HttpResponse("hello") # have given this response only for testing
当我尝试迭代或获取变量get_exams中的值时,出现了“抽象模型无法实例化”错误。请帮忙解决!
谢谢。
编辑:
我已经解决了问题。我执行了pip install djongo
,它将django的版本从3.2降级到3.0.5,这解决了该问题。
from djongo import models from djongo.models import ObjectIdField from django.contrib.postgres.fields import ArrayField
- Harish hari 53from djongo import models from djongo.models import ObjectIdField
这是我的views.py导入:from django.contrib.auth import authenticate, login, logout from django.contrib.auth.models import Group from .forms import UserForm from .decorators import unauthenticated_student, allowed_users from teacher.models import * from exams.models import * from exams.models import ExamDetails from django.http import HttpResponse, HttpResponseRedirect, Http404
- Harish hari 53