8

I am working on a project which has one abstract model and one main model for Djongo. When I try to insert a value, it is getting inserted without errors. But when I try to retrieve the data I get " Abstract models cannot be instantiated".

Here is my model:

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()

here is my code for querying:

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

When I try to iterate or get values in the variable get_exams I am getting "Abstract models cannot be instantiated" error. Please help!

Thanks

EDIT: I fixed it . I did pip install djongo which downgraded the version of django from 3.2 to 3.0.5 which fixed the issue.

  • 1
    you have all your migrations applied? – SergioC May 22 '21 at 15:48
  • Yes,all migrations have been applied . I have no issues inserting a record to the db. But retrieval is not working – Harish hari 53 May 22 '21 at 15:52
  • Can you show your imports? – GwynBleidD May 22 '21 at 15:55
  • This is models.py imports `from djongo import models from djongo.models import ObjectIdField from django.contrib.postgres.fields import ArrayField ` – Harish hari 53 May 22 '21 at 16:17
  • This is models.py imports `from djongo import models from djongo.models import ObjectIdField `This is my views.py import : `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 May 22 '21 at 16:23

2 Answers2

7

it will be by Downgrading your django version to 3.1.12 pip install django==3.1.12

azhar
  • 351
  • 3
  • 13
-2

Remove the Class Meta: abstract=True From Exam_questions class. And see

  • 1
    The Djongo documentation states "In case you don’t plan on using your embedded model as a standalone model (which means it will always be embedded inside a parent model) you should add the class Meta and abstract = True This way Djongo will never register this model as an actual model." Therefore, some of us need this and removing it only leads to another error. – tayoung Jan 30 '22 at 21:10