Skip to content

Commit 07525fe

Browse files
committed
more edits
1 parent fae14f2 commit 07525fe

File tree

3 files changed

+6
-85
lines changed

3 files changed

+6
-85
lines changed

django_mongodb_backend/fields/embedded_model.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ class EmbeddedModelField(models.Field):
1111

1212
def __init__(self, embedded_model, *args, **kwargs):
1313
"""
14-
`embedded_model` is the model class of the instance that will be
15-
stored. Like other relational fields, it may also be passed as a
16-
string.
14+
`embedded_model` is the model class of the instance to be stored.
15+
Like other relational fields, it may also be passed as a string.
1716
"""
1817
self.embedded_model = embedded_model
1918
super().__init__(*args, **kwargs)
@@ -48,8 +47,8 @@ def get_internal_type(self):
4847
def _set_model(self, model):
4948
"""
5049
Resolve embedded model class once the field knows the model it belongs
51-
to. If __init__()'s embedded_model argument is a string, resolve it to the
52-
corresponding model class, similar to relation fields.
50+
to. If __init__()'s embedded_model argument is a string, resolve it to
51+
the actual model class, similar to relation fields.
5352
"""
5453
self._model = model
5554
if model is not None and isinstance(self.embedded_model, str):
@@ -98,9 +97,6 @@ def get_db_prep_save(self, embedded_instance, connection):
9897
f"Expected instance of type {self.embedded_model!r}, not "
9998
f"{type(embedded_instance)!r}."
10099
)
101-
# Apply pre_save() and get_db_prep_save() of embedded instance
102-
# fields, create the field => value mapping to be passed to
103-
# storage preprocessing.
104100
field_values = {}
105101
add = embedded_instance._state.adding
106102
for field in embedded_instance._meta.fields:
@@ -112,7 +108,6 @@ def get_db_prep_save(self, embedded_instance, connection):
112108
continue
113109
field_values[field.attname] = value
114110
# This instance will exist in the database soon.
115-
# TODO: Ensure that this doesn't cause race conditions.
116111
embedded_instance._state.adding = False
117112
return field_values
118113

django_mongodb_backend/forms/fields/embedded_model.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class EmbeddedModelField(forms.MultiValueField):
3434

3535
def __init__(self, model, prefix, *args, **kwargs):
3636
form_kwargs = {}
37-
# The field must be prefixed with the name of the field.
37+
# To avoid collisions with other fields on the form, # each subfield
38+
# must be prefixed with the name of the field.
3839
form_kwargs["prefix"] = prefix
3940
self.form_kwargs = form_kwargs
4041
self.model_form_cls = modelform_factory(model, fields="__all__")

tests/schema_/test_embedded_model.py

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,6 @@ def delete_tables(self):
5959
table_names.remove(tbl)
6060
connection.enable_constraint_checking()
6161

62-
def get_indexes(self, table):
63-
"""
64-
Get the indexes on the table using a new cursor.
65-
"""
66-
with connection.cursor() as cursor:
67-
return [
68-
c["columns"][0]
69-
for c in connection.introspection.get_constraints(cursor, table).values()
70-
if c["index"] and len(c["columns"]) == 1
71-
]
72-
73-
def get_uniques(self, table):
74-
with connection.cursor() as cursor:
75-
return [
76-
c["columns"][0]
77-
for c in connection.introspection.get_constraints(cursor, table).values()
78-
if c["unique"] and len(c["columns"]) == 1
79-
]
80-
8162
def get_constraints(self, table):
8263
"""
8364
Get the constraints on a table using a new cursor.
@@ -93,41 +74,6 @@ def get_constraints_for_columns(self, model, columns):
9374
constraints_for_column.append(name)
9475
return sorted(constraints_for_column)
9576

96-
def check_added_field_default(
97-
self,
98-
schema_editor,
99-
model,
100-
field,
101-
field_name,
102-
expected_default,
103-
cast_function=None,
104-
):
105-
schema_editor.add_field(model, field)
106-
database_default = connection.database[model._meta.db_table].find_one().get(field_name)
107-
if cast_function and type(database_default) is not type(expected_default):
108-
database_default = cast_function(database_default)
109-
self.assertEqual(database_default, expected_default)
110-
111-
def get_constraints_count(self, table, column, fk_to):
112-
"""
113-
Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
114-
number of foreign keys, unique constraints, and indexes on
115-
`table`.`column`. The `fk_to` argument is a 2-tuple specifying the
116-
expected foreign key relationship's (table, column).
117-
"""
118-
with connection.cursor() as cursor:
119-
constraints = connection.introspection.get_constraints(cursor, table)
120-
counts = {"fks": 0, "uniques": 0, "indexes": 0}
121-
for c in constraints.values():
122-
if c["columns"] == [column]:
123-
if c["foreign_key"] == fk_to:
124-
counts["fks"] += 1
125-
if c["unique"]:
126-
counts["uniques"] += 1
127-
elif c["index"]:
128-
counts["indexes"] += 1
129-
return counts
130-
13177
def assertIndexOrder(self, table, index, order):
13278
constraints = self.get_constraints(table)
13379
self.assertIn(index, constraints)
@@ -136,27 +82,6 @@ def assertIndexOrder(self, table, index, order):
13682
all(val == expected for val, expected in zip(index_orders, order, strict=True))
13783
)
13884

139-
def assertForeignKeyExists(self, model, column, expected_fk_table, field="id"):
140-
"""
141-
Fail if the FK constraint on `model.Meta.db_table`.`column` to
142-
`expected_fk_table`.id doesn't exist.
143-
"""
144-
if not connection.features.can_introspect_foreign_keys:
145-
return
146-
constraints = self.get_constraints(model._meta.db_table)
147-
constraint_fk = None
148-
for details in constraints.values():
149-
if details["columns"] == [column] and details["foreign_key"]:
150-
constraint_fk = details["foreign_key"]
151-
break
152-
self.assertEqual(constraint_fk, (expected_fk_table, field))
153-
154-
def assertForeignKeyNotExists(self, model, column, expected_fk_table):
155-
if not connection.features.can_introspect_foreign_keys:
156-
return
157-
with self.assertRaises(AssertionError):
158-
self.assertForeignKeyExists(model, column, expected_fk_table)
159-
16085
def assertTableExists(self, model):
16186
self.assertIn(model._meta.db_table, connection.introspection.table_names())
16287

0 commit comments

Comments
 (0)