djongo icon indicating copy to clipboard operation
djongo copied to clipboard

[Help Wanted] Insert data into ArrayReferenceField inside EmbeddedModelField

Open magdot opened this issue 6 years ago • 4 comments

I've following models

class Address(models.Model):
	city = models.CharField(max_length=100) 
	district = models.CharField(max_length=100)
	zip_np = models.CharField(max_length=20)

class Info(models.Model):
	name = models.CharField(max_length=50)
	phone = models.CharField(max_length=12)
	addresses = models.ArrayReferenceField(to=Address)

	class Meta:
		abstract = True

class Customer(models.Model):
	company = models.CharField(max_length=20)
	personal_info = models.EmbeddedModelField(model_container=Info)

So how do I insert Address in the addresses field of Info model?

magdot avatar Jul 02 '18 09:07 magdot

Didn't read your title fully. But you cannot have a reference field inside an embedded field. The django models will not resolve the reverse reference in such a case. If you want to reference addresses. Move it to the parent, customer, which is not abstract. Having a reference filed in an abstract model will only work if you inherit it.

nesdis avatar Jul 02 '18 10:07 nesdis

Is there a path forward where we could have a reference field inside an embedded document? I use this all the time. Use case is storing a list of embedded documents that each have references to things like a Django user. I think not being able to do this greatly limits one of the benefits of MongoDB.

A real world use case would be storing a list of locations as embedded documents on a business. Each location has a list of references to services that a particular location provides.

Thoughts?

dblado avatar Jul 17 '18 00:07 dblado

Did you solve this problem? I need to insert a list of references inside the embedded documents. I did not have any problem with mongoengine, but I'd like to use djongo instead.

iserranoe avatar Jun 25 '19 14:06 iserranoe

@nesdis Fundamentally is this out/impossible? Or is there any chance something like this can be implemented?

drewgatchell avatar May 30 '21 01:05 drewgatchell