djongo
djongo copied to clipboard
adding FlexiEmbedField.linked to #624
Added a EmbeddedField supported some flexible behavior. It will not be always required to put the value over the model field. If you want to skip it put null=True, blank=True. Though current implementation will check all condition and decide if the field is skippable or not. If it is possible It will skip the field from the current instance while saving in database. This functionality will give you the full feature of document type database such as mongoDB. You can save it as a flexible document.
My Testing Model Structure 👍
class TestAbs(models.Model): desc = models.CharField(max_length=30,blank=False,null=False) desc1 = models.CharField(max_length=30,blank=False,null=False,default = "default values") desc2 = models.CharField(max_length=30,blank=True,null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
class TestEmbed(models.Model): _id = models.ObjectIdField() test_embed = FlexiEmbeddedField(model_container=TestAbs)
class Meta:
db_table = "TestEmbed"
def save(self, *args, **kwargs):
non_pks = [f for f in self._meta.local_concrete_fields if not f.primary_key]
raw= True
values = [(f, None, (getattr(self, f.attname) if raw else f.pre_save(self, False)))
for f in non_pks]
non_null_values_fields = []
try:
for f in non_pks:
field_value = getattr(self,f.attname)
if field_value is not None and field_value != "":
non_null_values_fields.append(f)
non_null_values_fields = tuple(non_null_values_fields)
self._meta.local_concrete_fields = non_null_values_fields
except :
pass
super().save(*args, **kwargs)
To save the model like this save method also should be overridden. In this current modeling desc2 of TestAbs can be skipped any time.