Django, assigning a composite value to one field depending upon values entered in other fields in same model?
Solution 1:
You can do this way overriding save method. Note that class variable clashesh with original builtin class name please migrate once as i changed the variable name
class Student(models.Model):
student_id = models.CharField(max_length=100, primary_key=True, editable=False)
name = models.CharField(max_length=100)
classes = models.CharField(max_length=200)
roll_no = models.CharField(max_length=500)
def save(self, *args, **kwargs):
self.student_id = f'{self.name}_{self.classes}_{self.roll_no}'
super(Student,self).save(*args, **kwargs)