djongo icon indicating copy to clipboard operation
djongo copied to clipboard

Foreign keys in embedded documents

Open halvorboe opened this issue 8 years ago • 2 comments

Foreign keys in embedded documents are not saved.

Python script

from djongo import models

class Institution(models.Model):
    name = models.CharField(max_length=100)

class Study(models.Model):

    institution = models.ForeignKey(Institution, on_delete=models.CASCADE, null=False)

    class Meta:
        abstract = True

class Form(models.Model):
    studies = models.ArrayModelField(
        model_container=Study,
    )

Traceback

There are no errors. The problem is that the relation between study and instituion is allowed when you call form.save(), but not actually added to the database.

I used MongoEngine before, and this works just fine with that package.

halvorboe avatar Apr 10 '18 20:04 halvorboe

Looking into the code, I migth have found the problem.

fields.py

# ArrayModelField -> get_db_prep_value

for fld in a_mdl._meta.get_fields():
    if not useful_field(fld):
        continue
    fld_value = getattr(a_mdl, fld.attname)
    mdl_ob[fld.attname] = fld.get_db_prep_value(fld_value, connection, prepared)
    ret.append(mdl_ob)

def useful_field(field):
    return field.concrete and not (field.is_relation or isinstance(field, (AutoField, BigAutoField)))

field.is_relation = True

Then the field would be ignored when converting preparing from what I could get from https://docs.djangoproject.com/en/2.0/howto/custom-model-fields/#converting-query-values-to-database-values. Because the model is abstract and not in the migrations, it never is added to the query. That is just a possiblity, I'm not too sure about this.

halvorboe avatar Apr 11 '18 13:04 halvorboe

#City Model Starts here class City(models.Model): state = models.ForeignKey(State, on_delete=models.CASCADE, verbose_name=("State Name")) city_name = models.CharField(max_length=100, verbose_name=("City Name"))

def __str__(self):
    return self.city_name

class Meta:
    verbose_name = "City"
    verbose_name_plural = "City"
    db_table = 'city'

#Contact Additional Profile using as embedded field in AdditionalProfile Model class ContactAdditionalProfile(models.Model): mobile_no = models.BigIntegerField( verbose_name=("Mobile Number ")) city = models.ForeignKey(City, blank=True, null=True, on_delete=models.CASCADE, verbose_name=("City")) class Meta: abstract = True

#Additional Profile Model class AdditionalProfile(models.Model): contact = models.EmbeddedField( model_container=ContactAdditionalProfile, ) user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name=_("User")) objects = models.DjongoManager()

def __str__(self):
    return self.user.username

class Meta:
    verbose_name = "Additional Profile"
    verbose_name_plural = "Additional Profile"
    db_table = 'user_addtional_profile'

I am also getting same problem but i am using EmbeddedField(contact) and every time ForeignKey field(city) saving as null. Please suggest if i am doing wrong. Thanks & Regards Mahesh

ghost avatar Jun 20 '20 12:06 ghost