Skip to content

Commit

Permalink
Support for Django 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nesdis committed May 3, 2020
1 parent 45f1c68 commit d411f26
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 286 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: python

python:
- "3.6"
- "3.7"
- "3.8"

install:
Expand All @@ -18,4 +17,4 @@ before_script:
- mongo --version

script:
- tox -e $(echo py$TRAVIS_PYTHON_VERSION | tr -d .)-django_stable
- tox -e py36-django_stable,py38-django30
28 changes: 21 additions & 7 deletions djongo/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from django.db.models import (
Manager, Model, Field, AutoField,
ForeignKey, BigAutoField)
from django.utils import version
from django.db.models.fields.related import RelatedField
from django.forms import modelform_factory
from django.utils.functional import cached_property
Expand All @@ -33,6 +34,11 @@
from django.utils.translation import gettext_lazy as _
from djongo.exceptions import NotSupportedError, print_warn

django_major = int(version.get_version().split('.')[0])

if django_major >= 3:
from django.db.models.fields import AutoFieldMixin, AutoFieldMeta


def make_mdl(model, model_dict):
"""
Expand Down Expand Up @@ -217,7 +223,7 @@ def get_prep_value(self, value):
value)
return processed_value

def from_db_value(self, value, expression, connection, context):
def from_db_value(self, value, *args):
return self.to_python(value)

def to_python(self, value):
Expand Down Expand Up @@ -593,6 +599,7 @@ def value_omitted_from_data(self, data, files, name):

class ObjectIdFieldMixin:
description = _("ObjectId")
empty_strings_allowed = False

def get_db_prep_value(self, value, connection, prepared=False):
return self.to_python(value)
Expand All @@ -605,12 +612,23 @@ def to_python(self, value):
def get_internal_type(self):
return "ObjectIdField"

def rel_db_type(self, connection):
return self.db_type(connection)


class GenericObjectIdField(ObjectIdFieldMixin, Field):
empty_strings_allowed = False
pass


class ObjectIdField(ObjectIdFieldMixin, AutoField):
if django_major >= 3:
class _ObjectIdField(AutoFieldMixin, GenericObjectIdField, metaclass=AutoFieldMeta):
pass
else:
class _ObjectIdField(ObjectIdFieldMixin, AutoField):
pass


class ObjectIdField(_ObjectIdField):
"""
For every document inserted into a collection MongoDB internally creates an field.
The field can be referenced from within the Model as _id.
Expand All @@ -624,10 +642,6 @@ def __init__(self, *args, **kwargs):
id_field_args.update(kwargs)
super().__init__(*args, **id_field_args)

def get_prep_value(self, value):
value = super(AutoField, self).get_prep_value(value)
return value


class ArrayReferenceManagerMixin:

Expand Down
4 changes: 1 addition & 3 deletions djongo/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from django.utils import timezone
import datetime
import calendar
import six



class DatabaseOperations(BaseDatabaseOperations):
Expand Down Expand Up @@ -50,7 +48,7 @@ def adapt_timefield_value(self, value):
if value is None:
return None

if isinstance(value, six.string_types):
if isinstance(value, str):
return datetime.datetime.strptime(value, '%H:%M:%S')

if timezone.is_aware(value):
Expand Down
9 changes: 4 additions & 5 deletions docs/_data/navigation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ main:
url: https://github.com/nesdis/djongo/

docs:
- title: "Get started"
url: /get-started/

- title: "Using Djongo"
url: /integrating-django-with-mongodb/
- title: "Get started"
url: /get-started/
- title: "Djongo Fields"
children:
- title: "Embedded Field"
Expand All @@ -24,10 +23,10 @@ docs:
- title: "Array Reference Field"
url: /using-django-with-mongodb-array-reference-field/
- title: "Other fields"
url: /using-django-with-objectid-field/
url: /using-django-with-other-fields/

- title: "DjongoNxt"
url: /djongonxt/
# url: /djongonxt/
children:
- title: "Indexes"
url: /djongonxt-indexes/
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ e.save()

```

## Validating field data
## Field data integrity checks

Djongo automatically validates the value assigned to an EmbeddedField. Validation criteria (`null=True` or `blank=False`) can be applied on the `ÈmbeddedField` or to the internal fields (`CharField`)
Djongo automatically validates the value assigned to an EmbeddedField. Integrity criteria (`null=True` or `blank=False`) can be applied on the `ÈmbeddedField` or to the internal fields (`CharField`)

```python
class Entry(models.Model):
Expand Down Expand Up @@ -102,6 +102,7 @@ e.clean_fields()
>>>
ValidationError({'blog': ['This field cannot be null.']})
```

## Nesting Embedded Fields

An `EmbeddedField` or `ArrayField` can be nested inside an `EmbeddedField`. There is no limitation on the depth of nesting.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Other fields
permalink: /using-django-with-objectid-field/
permalink: /using-django-with-other-fields/
---

## The ObjectId Field
Expand Down
Loading

0 comments on commit d411f26

Please sign in to comment.