class MyModel(models.Model):
field1 = models.ForeignKey('AnotherModel', null=False, blank=False)
field2 = #its a choice field
# some more fields
What I want is to fill the field1
with a default value if field2
is equal to some specific value (from choice tuple).
I gave it a try by overriding the save()
method but I wasn't successful.
def save(self, *args, *kwargs):
if self.field2 == 'some-specific-value':
self.field1 = 'some-default-value or a dummy object id?'
For other values of choice field (field2
) user has to provide a value but as mentioned above, for a specific value we will fill with default word/string.
I even tried to use signal pre_save
, pre_init
, post_init
but no success
@receiver(post_init, sender=MyModel)
def populate_field1(sender, instance, *args, **kwargs):
print 'in signal'
if instance.field2 == 'some-specific-value':
instance.field1 = 'some-default-value'
How can I do this?