Problem with multi-table inheritance in Django

0

I have a problem implementing multi-table inheritance in Django. In my file models.py I have the following model that is the father:

class Concepto(models.Model):
    id_concepto = models.IntegerField()

And these are his children:

class Estructura(Concepto):
    estructura_padre = models.ForeignKey('self', on_delete=models.CASCADE)


class Usuario(Concepto):
    estructura = models.ManyToManyField(Estructura)

However defining the inheritance in this way produces an error. First of all I have to include the following import in the models.py class (where these models come):

from calendario.models import Concepto

But in the multi-table inheritance examples that come in the Django 1.11.1 documentation (the one I'm using) does not have that import, they simply define the parent model and then the child:

from django.db import models

class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

class Restaurant(Place):
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

As you can see in the example, you do not have to import the parent model.

EDITED

When I try to execute the makemigrations command I get the following error:

   Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 337, in execute
    django.setup()
  File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models()
  File "/usr/local/lib/python3.5/dist-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/app/calendario/models.py", line 3, in <module>
    from calendario.models import Concepto
ImportError: cannot import name 'Concepto'

As you can see, I get an error in models.py when I import the Concept model (the parent model) and it tells me that it can not do that import. Can you tell me what I'm doing wrong? Thanks in advance.

EDITED

Then I leave you with the structure of the project:

proyecto
    calendario
        __init__.py
        admin.py
        apps.py
        models.py (aqui esta el modelo padre Concepto y los hijos Estructura y Usuario)
        serializers.py (tengo hecho un serializer para Concepto)
        tests.py
        urls.py
        views.py (aqui tengo algunas vistas que manejan Concepto por tanto tengo que importarlo)
    
asked by Ethan 16.06.2017 в 16:45
source

1 answer

0

Wow, I just discovered the error and I really feel very silly because it's been a beginner's mistake in Django. Anyway, what happens is that I was declaring the daughter classes and later the father class, that is:

class Estructura(Concepto):
    estructura_padre = models.ForeignKey('self', on_delete=models.CASCADE)

class Usuario(Concepto):
    estructura = models.ManyToManyField(Estructura)

class Concepto(models.Model):
    id_concepto = models.IntegerField()

With this in mind, fix the rest of my models.py file and now everything works wonders.

    
answered by 16.06.2017 / 19:39
source