String Reference not working in model container when using Djongo models

11 views Asked by At

I want to reference CartCustomizationCategory class inside CartCustomization, but the CartCustomizationCategory class is defined below CartCustomization class. I can't change the order of the classes because they are used inside each other. So, I used string reference which works with Django models, but isn't working with Djongo models. Is there a python way to solve this, or I can use something else for this.

class CartCustomization(models.Model):
    class Meta:
        abstract = True
    
    id = models.CharField(max_length=300)
    name = models.CharField(max_length=100)
    quantity = models.IntegerField()
    price = models.IntegerField()
    customization_categories = models.ArrayField(model_container = "CartCustomizationCategory")


class CartCustomizationCategory(models.Model):
    class Meta:
        abstract = True

    id = models.CharField(max_length=300)
    name = models.CharField(max_length=100)
    customizations = models.ArrayField(model_container=CartCustomization)

class CartItem(models.Model):
    class Meta:
        abstract = True

    id = models.CharField(max_length=300)
    name = models.CharField(max_length=100)
    parent_item_id = models.CharField(max_length=12)
    quantity = models.IntegerField()
    price = models.IntegerField()
    customization_categories = models.ArrayField(model_container=CartCustomizationCategory)

This is the Traceback Stack

Traceback (most recent call last):
  File "D:\CRUV\craver-backend\craver\manage.py", line 22, in <module>
    main()
  File "D:\CRUV\craver-backend\craver\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\django\core\management\__init__.py", line 416, in execute
    django.setup()
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\django\apps\registry.py", line 116, in populate
    app_config.import_models()
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\django\apps\config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\rudra\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "D:\CRUV\craver-backend\craver\cart\models.py", line 6, in <module>
    class CartCustomization(models.Model):
  File "D:\CRUV\craver-backend\craver\cart\models.py", line 14, in CartCustomization
    customization_categories = models.ArrayField(model_container = "CartCustomizationCategory")
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\djongo\models\fields.py", line 276, in __init__
    super().__init__(model_container, *args, **kwargs)
  File "D:\CRUV\craver-backend\venv\Lib\site-packages\djongo\models\fields.py", line 114, in __init__
    self.model_container._meta.abstract = False
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '_meta'

I tried using string reference for this which works with Django models, but it didn't work.

0

There are 0 answers