-
Type:
New Feature
-
Resolution: Unresolved
-
Priority:
Unknown
-
Affects Version/s: None
-
Component/s: django
-
None
Context
Currently, if a user wants to define an index on an embedded field within a model, they have to do this within the `EmbeddedModel` abstraction. Conceptually this doesn't make sense as EmbeddedModels are not collections themselves, so they allow for reusability between top-level models. We did this at first because top level models validate the existence of a field and there existed built-in method to validate embedded fields prior to the django-mongodb-backend. We need to change this to support index definitions at top-level embedded models.
class Address(EmbeddedModel): unique_constraint_one = models.CharField(max_length=10) class Author(EmbeddedModel): address = EmbeddedModelField(Address) unique_constraint_two = models.CharField(max_length=10) class Book(models.Model): author = EmbeddedModelField(Author) class Meta: constraints = [ models.UniqueConstraint( fields=["author.unique_constraint_two"], name="unique_two", ), models.UniqueConstraint( fields=["author.address.unique_constraint_one"], name="unique_one", ), ]
Definition of done
Change the above to support this syntax:
class Address(EmbeddedModel): unique_together_one = models.CharField(max_length=10) unique_together_two = models.CharField(max_length=10) class Meta: app_label = "schema_" class Author(EmbeddedModel): address = EmbeddedModelField(Address) unique_together_three = models.CharField(max_length=10) unique_together_four = models.CharField(max_length=10) class Meta: app_label = "schema_" class Book(models.Model): author = EmbeddedModelField(Author) class Meta: app_label = "schema_" constraints = [ models.UniqueConstraint( F("author__unique_together_three").asc(), F("author__unique_together_four").desc(), name="unique_together_34", ), ( models.UniqueConstraint( F("author_address_unique_together_one"), F("author_address_unique_together_two").asc(), name="unique_together_12", ) ), ]
Pitfalls
Using `F` expressions may be cumbersome in the eyes of the end-user. As a mitigation we could implement a light wrapper (afterwards) that converts dot notation (mongodb's standard way of identifying subfields) into `__` notation.