Will Django model instance always be populated when calling save()?
I have a model where I am overriding the save() function to add SearchVector's. I need to preprocess one of the instance's fields so I am overriding the save function:
class Student(models.Model):
first_name = models.CharField(max_length=255, default="")
last_name = models.CharField(max_length=255, default="")
search_vector = SearchVectorField(null=True)
def save(self, *args, **kwargs):
sv = SearchVector(Value(self.first_name)) + SearchVector(Value(self.last_name))
if len(self.last_name) > 3:
sv += SearchVector(Value(self.last_name[0:3]))
self.search_vector = sv
super().save(*args, **kwargs)
Can I expect that all fields (first_name & last_name) of the instance are always populated inside the save() function? I noticed that the instance loading function from_db seems to suggest that model instances can be created with a partial set of fields. Does this mean that first_name could be None on the save() call?