Document how to configure contrib apps with models

XMLWordPrintableJSON

    • 🔵 Done
    • Python Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Hi Team,

      I'm quite new to Django but would like to use the django-mongodb-backend in conjunction with django.contrib.sites.

      I have the following model defined:

      from django.contrib.sites.models import Site
      from django.db import models
      
      
      class Page(models.Model):
          title = models.CharField(max_length=50)
          content = models.TextField()
          site = models.ForeignKey(Site, on_delete=models.CASCADE)
      
      
          def __str__(self):
              return self.title

      When I go to create a site, it creates successfully. However, I cannot associate the site to an instance of Page:

      >>> from django.contrib.sites.models import Site                                                                                                                                                                                              >>> from website.models import Page                                                                                                                                                                                                           >>> from bson import ObjectId                                                                                                                                                                                                                 >>> s = Site(name='test', domain='test.com')                                                                                                                                                                                                  >>> s.save()                                                                                                                                                                                                                                  >>> p = Page(title='test', content='test', site=s)                                                                                                                                                                                            >>> s.save()Traceback (most recent call last):                                                                                                                                                                                                              File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/fields/__init__.py", line 2128, in get_prep_value                                                                                                 return int(value)                                                                                                                                                                                                                         TypeError: int() argument must be a string, a bytes-like object or a real number, not 'ObjectId'                                                                                                                                                                                                                                                                                                                                                                                            The above exception was the direct cause of the following exception:                                                                                                                                                                                                                                                                                                                                                                                                                        Traceback (most recent call last):                                                                                                                                                                                                              File "<console>", line 1, in <module>                                                                                                                                                                                                         File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 902, in save                                                                                                                       self.save_base(                                                                                                                                                                                                                               ~~~~~~~~~~~~~~^                                                                                                                                                                                                                                   using=using,                                                                                                                                                                                                                                  ^^^^^^^^^^^^                                                                                                                                                                                                                              ...<2 lines>...                                                                                                                                                                                                                                   update_fields=update_fields,                                                                                                                                                                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                                              )                                                                                                                                                                                                                                             ^                                                                                                                                                                                                                                           File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/base.py", line 988, in save_base                                                                                                                  pre_save.send(    ~~~~~~~~~~~~~^        sender=origin,        ^^^^^^^^^^^^^^    ...<3 lines>...        update_fields=update_fields,        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^    )    ^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/dispatch/dispatcher.py", line 189, in send    response = receiver(signal=self, sender=sender, **named)  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/contrib/sites/models.py", line 114, in clear_site_cache    del SITE_CACHE[Site.objects.using(using).get(pk=instance.pk).domain]                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/query.py", line 619, in get    clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)                                                        ~~~~~~~~~~~^^^^^^^^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/query.py", line 1493, in filter    return self._filter_or_exclude(False, args, kwargs)           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/query.py", line 1511, in _filter_or_exclude    clone._filter_or_exclude_inplace(negate, args, kwargs)    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/query.py", line 1518, in _filter_or_exclude_inplace    self._query.add_q(Q(*args, **kwargs))    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/sql/query.py", line 1646, in add_q    clause, _ = self._add_q(q_object, can_reuse)                ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/sql/query.py", line 1678, in _add_q    child_clause, needed_inner = self.build_filter(                                 ~~~~~~~~~~~~~~~~~^        child,                                                                                                                 ^^^^^^                                                                                                             ...<7 lines>...                                                                                                                                                                                                                                   update_join_types=update_join_types,                                                                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                               )                                                                                                                                                                                                                                             ^                                                    File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/sql/query.py", line 1588, in build_filter    condition = self.build_lookup(lookups, col, value)                                                                                                                                                                                          File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/sql/query.py", line 1415, in build_lookup    lookup = lookup_class(lhs, rhs)                         File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/lookups.py", line 38, in __init__                      self.rhs = self.get_prep_lookup()                   ~~~~~~~~~~~~~~~~~~~~^^      File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/lookups.py", line 410, in get_prep_lookup    return super().get_prep_lookup()                       ~~~~~~~~~~~~~~~~~~~~~~~^^              File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/lookups.py", line 96, in get_prep_lookup    return self.lhs.output_field.get_prep_value(self.rhs)           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^  File "/Users/mattt/projects/python/django/.venv/lib/python3.13/site-packages/django/db/models/fields/__init__.py", line 2130, in get_prep_value    raise e.__class__(        "Field '%s' expected a number but got %r." % (self.name, value),    ) from e                                TypeError: Field 'id' expected a number but got ObjectId('68efa83fe75201e9e0a7ad37').

      The site was created as follows:

      quickstart> db.django_site.find()
      [
        {
          _id: ObjectId('68efa83fe75201e9e0a7ad37'),
          domain: 'test.com',
          name: 'test'
        }
      ]
      quickstart>  

      Am I missing any steps to "activate" django.contrib.sites when using the django-mongodb-backend?

      Thank you,
      Matt

            Assignee:
            Tim Graham
            Reporter:
            Matt Thompson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: