doableware/djongo

Foreign Key in Embedded Field model

bluespider42 opened this issue · 1 comments

One line description of the issue

Cannot have a Foreign Key as a field in an Embedded Field model.

Python script

class TestForeignKey(models.Model):
    _id = models.ObjectIdField()
    my_field = models.TextField(default='', blank=True)


class TestSubModel(models.Model):
    my_foreign_key = models.ForeignKey(to=TestForeignKey, on_delete=models.SET_NULL)
    other_field = models.TextField(default='', blank=True)

    class Meta:
        abstract = True


class TestModel(models.Model):
    _id = models.ObjectIdField()
    sub_model = models.EmbeddedField(model_container=TestSubModel)

Expected

A document similar to:

# TestForeignKey
{
    "_id": ObjectID,
    "my_field": "text here",
}

# TestModel
{
    "_id": ObjectID,
    "sub_model": {
        "my_foreign_key": ObjectID,
        "other_field": "some text",
    }
}

Traceback

django.core.exceptions.ValidationError: ['Field "my_app.TestSubModel.my_foreign_key" of model container:"<class \'my_app.models.TestSubModel\'>" cannot be of type "<class \'django.db.models.fields.related.ForeignKey\'>"']

It seems this may not be possible - but I'm not sure what the recommeded solution to this would be.

https://stackoverflow.com/questions/63494891/django-foreignkey-in-mongodbs-embeddedfield/67703706