doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.89k stars 355 forks source link

Error when adding an ArrayModelField to models. #415

Open Jagl257 opened 4 years ago

Jagl257 commented 4 years ago

Error when adding an ArrayModelField to models.

Following djongo tutorial: https://djongo.readthedocs.io/docs/array-model-field/

Using:

Python script

in models.py django project

 from djongo import models

 class succesfull_injections(models.Model):
      sqli = models.CharField(max_length=100)

      class Meta: 
          abstract = True

  class attack(models.Model):
      succesfullInjections = models.ArrayModelField(
              model_container = succesfull_injections,
              )
      successSQLI = models.CharField(max_length=100)
      time = models.CharField(max_length = 100)
        triedInjections = models.CharField(max_length = 100)

Traceback

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/jagl/.virtualenvs/django-getting-started/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/jagl/.virtualenvs/django-getting-started/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/home/jagl/.virtualenvs/django-getting-started/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/jagl/.virtualenvs/django-getting-started/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/jagl/.virtualenvs/django-getting-started/lib/python3.6/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/jagl/Documents/Learning/Programming/Django/django-getting-started/meeting_planner/reports/models.py", line 27, in <module>
    class attack(models.Model):
  File "/home/jagl/Documents/Learning/Programming/Django/django-getting-started/meeting_planner/reports/models.py", line 28, in attack
    succesfullInjections = models.ArrayModelField(
AttributeError: module 'djongo.models' has no attribute 'ArrayModelField'

StackOverflow question : https://stackoverflow.com/questions/62026138/djongo-models-has-no-attribute-arraymodelfield

Jagl257 commented 4 years ago

Solved this one, the documentation in this link is mistaken.

The problem is that it is not ArrayModelField but only ArrayField

class attack(models.Model):
      succesfullInjections = models.ArrayModelField(
              model_container = succesfull_injections,
              )
      successSQLI = models.CharField(max_length=100)
      time = models.CharField(max_length = 100)
        triedInjections = models.CharField(max_length = 100)

The correct would be:


class attack(models.Model):
      succesfullInjections = models.ArrayField(
              model_container = succesfull_injections,
              )
      successSQLI = models.CharField(max_length=100)
      time = models.CharField(max_length = 100)
        triedInjections = models.CharField(max_length = 100)

@nesdis I think this link may be deprecated.

Now I'm having the following issue #417

amit-jha-cmd commented 4 years ago

@Jagl257 I think you are right. I realised one more issue while trying to make ArrayField work. If your succesfull_injections has only 1 attribute you won't get any errors what so ever with ArrayField. However, If you have multiple attributes/objects inside of succesful_injection you'll get the same error as #417 @nesdis

amit-jha-cmd commented 4 years ago

Hey @Jagl257, solved this issue here

Invincible166 commented 4 years ago

Its strange. Actually I am getting this error

AttributeError: module 'djongo.models' has no attribute 'ArrayField'

However with ArrayModelField I get a different error.

TypeError at /speechAPI/ isinstance() arg 2 must be a type or tuple of types

Here is my code if anyone could help.

from appconf import AppConf
from djongo import models
from django import forms

class Words(models.Model):
    duration = models.DurationField() 
    offset = models.TimeField() 

    class Meta:
        abstract = True

class WordsForm(forms.ModelForm):
    class Meta:
        model = Words
        fields = ('duration', 'offset')

class Transcript(models.Model):
    display_text = models.TextField()
    words = models.ArrayModelField(model_container=Words,model_form_class=WordsForm)

    class Meta:
        abstract = True

class Transcription(models.Model):
    request_id = models.CharField(max_length=100)
    q_id = models.BigIntegerField()
    source = models.CharField(max_length=100)
    transcript = models.EmbeddedModelField(model_container=Transcript)

    objects = models.DjongoManager()

Any insight into the error would be greatly appreciated.

amit-jha-cmd commented 4 years ago

Can you mention the version of Djongo and Django you are on?

Invincible166 commented 4 years ago

Django==2.2.12 Djongo==1.2.38

Invincible166 commented 4 years ago

I have now upgraded djongo to 1.3.2. Now whenever I try to use

models.EmbeddedField(model_container=Transcript)

It gives the following error:

raise AppRegistryNotReady("Models aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

Jagl257 commented 4 years ago

Hi @Invincible166 I'm having the same issue. See #417