I am using Djongo engine for MongoDB in my Django project.
I have two tables
# app/models.py
# My Main Table
class Questions(models.Model):
questionId = models.UUIDField(default=uuid.uuid4, editable=True, unique=True)
question = models.TextField()
answer = models.EmbeddedField(
model_container=Answers
)
date = models.DateTimeField(auto_now_add=True, blank=True)
User = models.ForeignKey(UserDetailTable,on_delete=models.CASCADE,related_name='userinfo')
and
# app/models.py
# This table needs to be Embedded in Questions Table
class Answers(models.Model):
answerId = models.UUIDField(default=uuid.uuid4, editable=True, unique=True)
answer = models.TextField()
date = models.DateTimeField(auto_now_add=True)
User = models.ForeignKey(UserDetailTable,on_delete=models.CASCADE)
class Meta:
abstract = True
I want to embed Answers
in the Questions
table.
But I am getting this error
django.core.exceptions.ValidationError: ['Field "App.Answers.User" of model container:"<class \'App.models.Answers\'>" cannot be of type "<class \'django.db.models.fields.related.ForeignKey\'>"']
I know this error is because I am using User = models.ForeignKey(UserDetailTable,on_delete=models.CASCADE)
in Answers
which is itself an EmbeddedField on Questions
.
How Can I solve this error?
As there will be multiple answers from different users on the same question and with ForeignKey it will be simple to show the user's info along with his/her answer.
I have also looked djongo's docs but couldn't find anything.
Any help would be appreciated.