[Answered ]-How to autofill fields in django

3👍

The answer is: don’t bother.

Whenever you can generate some item of data based on the content of one or more fields on the table, you do not need to create another field for it. And your name_doctor + last_doctor is a classic example of this.

Your model already has a name_doctor field as well as a last_doctor field, it’s a trivial matter to display them by combining them together. There is no need to save it to the database and you shouldn’t.

Edit: You can produce any custom combination of fields by just adding a method to the model example:

class Doctor(models.Model):


     def concat_fields(self):
          return self.name_doctor + self.last_doctor

     def quasi_unique_id(self):
          return self.name_doctor[0:2] + self.last_doctor[0:2] + id_order(some_other_model)
👤e4c5

-1👍

SOLVED, i just added this before my post.save on my form def on my views.py

post.id_doctor = form['name_doctor'].value() + form['last_doctor'].value()
                post.save()

Leave a comment