E
E
Enter_a_nickname2022-02-07 13:08:07
Django
Enter_a_nickname, 2022-02-07 13:08:07

Why does Django throw TypeError: Abstract models cannot be instantiated?

I have a django 4.0.1 project in which I use the mondodb database. To connect with the database, I use the "djOngo" engine. The database connection is working correctly.

In my project, I need to create models nested in other models in models.py I turned to the official documentation of the "djOngo" engine and tried to implement an example that potentially closes my need, here it is:

from djongo import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    class Meta:
        abstract = True

class MetaData(models.Model):
    pub_date = models.DateField()
    mod_date = models.DateField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    class Meta:
        abstract = True

class Author(models.Model):
    name = models.CharField(max_length=200)
    email = models.EmailField()

    class Meta:
        abstract = True
        
    def __str__(self):
        return self.name

class Entry(models.Model):
    blog = models.ArrayField(
        model_container=Blog,
    )
    meta_data = models.ArrayField(
        model_container=MetaData,
    )

    headline = models.CharField(max_length=255)
    body_text = models.TextField()

    authors = models.ArrayField(
        model_container=Author,
    )
    n_comments = models.IntegerField()

    def __str__(self):
        return self.headline

    class Meta:
    verbose_name = 'Entry'
    verbose_name_plural = 'Entry'


After that, I registered the "Entry" class in the admin.py file:

from django.contrib import admin
from .models import Entry

admin.site.register(Entry)


And I tried to fill in the data through the admin panel. But when I go to the fill page, I get the following error: TypeError: Abstract models cannot be instantiated.

How can I fix this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-02-07
@Enter_a_nickname

Well, everything is written in the text of the error - you cannot call an instance of the abstract model. And the author is precisely that abstract.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question