Skip to content

Commit

Permalink
field name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham authored and WaVEV committed Jan 18, 2025
1 parent 11754df commit 47221b4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
27 changes: 23 additions & 4 deletions django_mongodb_backend/fields/embedded_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core import checks
from django.core.exceptions import FieldDoesNotExist
from django.db import models
from django.db.models.fields.related import lazy_related_operation
from django.db.models.lookups import Transform
Expand Down Expand Up @@ -112,7 +113,8 @@ def get_transform(self, name):
transform = super().get_transform(name)
if transform:
return transform
return KeyTransformFactory(name)
field = self.embedded_model._meta.get_field(name)
return KeyTransformFactory(name, field)

def validate(self, value, model_instance):
super().validate(value, model_instance)
Expand All @@ -134,9 +136,25 @@ def formfield(self, **kwargs):


class KeyTransform(Transform):
def __init__(self, key_name, *args, **kwargs):
def __init__(self, key_name, ref_field, *args, **kwargs):
super().__init__(*args, **kwargs)
self.key_name = str(key_name)
self.ref_field = ref_field

def get_transform(self, name):
result = None
if isinstance(self.ref_field, EmbeddedModelField):
opts = self.ref_field.embedded_model._meta
new_field = opts.get_field(name)
result = KeyTransformFactory(name, new_field)
else:
if self.ref_field.get_transform(name) is None:
raise FieldDoesNotExist(
f"{self.ref_field.model._meta.object_name}.{self.ref_field.name}"
f" has no field named '{name}'"
)
result = KeyTransformFactory(name, self.ref_field)
return result

def preprocess_lhs(self, compiler, connection):
key_transforms = [self.key_name]
Expand All @@ -154,8 +172,9 @@ def as_mql(self, compiler, connection):


class KeyTransformFactory:
def __init__(self, key_name):
def __init__(self, key_name, ref_field):
self.key_name = key_name
self.ref_field = ref_field

def __call__(self, *args, **kwargs):
return KeyTransform(self.key_name, *args, **kwargs)
return KeyTransform(self.key_name, self.ref_field, *args, **kwargs)
8 changes: 8 additions & 0 deletions tests/model_fields_/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class Data(models.Model):
integer = models.IntegerField(db_column="custom_column")
auto_now = models.DateTimeField(auto_now=True)
auto_now_add = models.DateTimeField(auto_now_add=True)
json_value = models.JSONField(default=dict)


class Address(models.Model):
Expand All @@ -119,3 +120,10 @@ class Author(models.Model):
class Book(models.Model):
name = models.CharField(max_length=100)
author = EmbeddedModelField(Author)


class Library(models.Model):
name = models.CharField(max_length=100)
books = models.ManyToManyField("Book", related_name="libraries")
location = models.CharField(max_length=100, null=True, blank=True)
best_seller = models.CharField(max_length=100, null=True, blank=True)
12 changes: 11 additions & 1 deletion tests/model_fields_/test_embedded_model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.core.exceptions import ValidationError
from django.core.exceptions import FieldDoesNotExist, ValidationError
from django.db import models
from django.test import SimpleTestCase, TestCase
from django.test.utils import isolate_apps
Expand Down Expand Up @@ -104,6 +104,16 @@ def test_nested(self):
)
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])

def test_nested_not_exists(self):
msg = "Address.city has no field named 'president'"
with self.assertRaisesMessage(FieldDoesNotExist, msg):
Book.objects.filter(author__address__city__president="NYC")

def test_not_exists_in_embedded(self):
msg = "Address has no field named 'floor'"
with self.assertRaisesMessage(FieldDoesNotExist, msg):
Book.objects.filter(author__address__floor="NYC")


@isolate_apps("model_fields_")
class CheckTests(SimpleTestCase):
Expand Down

0 comments on commit 47221b4

Please sign in to comment.