[Answer]-Django-nonrel: add element in LisftField of ForeignKeys

1👍

According to the Django MongoDB Engine documentation, it suggests to use EmbeddedModel from djangotoolbox:

from djangotoolbox.fields import ListField, EmbeddedModelField

class Post(models.Model):
    ...
    comments = ListField(EmbeddedModelField('Comment'))

class Comment(models.Model):
    text = models.TextField()

Edit: Forgot the link: http://django-mongodb-engine.readthedocs.org/en/latest/topics/embedded-models.html

👤jliles

0👍

I actually just figured out what was wrong.
It is apparently impossible to declare the foreign key class type as a string when in a Listfield. Weird…
If it happens to you, just do the following change:

list = ListField(models.ForeignKey('AWUser'))

becomes:

list = ListField(models.ForeignKey(AWUser))

If anyone as a good explanation of what is going on, I’d love to hear it 🙂

Leave a comment