Argument to ReferenceField constructor must be a document class or a string

19 views
Skip to first unread message

Abdulla Ansari

unread,
Jan 18, 2021, 2:08:32 AM1/18/21
to MongoEngine Users

class ObjectSeen(Document):
    user = fields.LazyReferenceField('User', null=True, dbref=True)
    ip_address = fields.StringField()
    content_type = fields.ReferenceField(ContentType)
    object_id = fields.IntField()
    content_object = fields.GenericEmbeddedDocumentField('content_type', 'object_id')
    timestamp = fields.DateTimeField(default=datetime.now)


This is my model where I am implementing the fields and for the user field it is throwing errors,

 File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 110, in inner_run
    autoreload.raise_last_exception()
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 76, in raise_last_exception
    raise _exception[1]
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
    autoreload.check_errors(django.setup)()
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/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/bluethink/Desktop/Abdulla/allauthlib/analytics/models.py", line 50, in <module>
    class ObjectSeen(Document):
  File "/home/bluethink/Desktop/Abdulla/allauthlib/analytics/models.py", line 53, in ObjectSeen
    content_type    = fields.ReferenceField(ContentType)
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/mongoengine/fields.py", line 1189, in __init__
    "Argument to ReferenceField constructor must be a "
  File "/home/bluethink/Desktop/Abdulla/allauthlib/env/lib/python3.6/site-packages/mongoengine/base/fields.py", line 167, in error
    raise ValidationError(message, errors=errors, field_name=field_name)
mongoengine.errors.ValidationError: Argument to ReferenceField constructor must be a document class or a string

Note: I am using Django's custom user model
Thanks in Advance
Reply all
Reply to author
Forward
0 new messages