Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding FlexiEmbedField.linked to #624 #628

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

keder-code-hash
Copy link

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.

@keder-code-hash
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant