Django How to generate value of attribute while initializing
I have a enum table ServiceType in my database which includes "Credit", "Investment" and "Insurance" and another table Service which have two columns - type_id and type. Is there a way to call method which will be generating value of type from value of type_id while initialization?
Should I use __init__ or maybe save?
type in Service is a ForeignKey to ServiceType
Thanks, sorry for my bad English.
So, I've tried something like this:
class Service(models.Model): type_id = models.CharField(..) type = models.ForeignKey('ServiceType', to_field = 'type', default = foo(type_id)) class ServiceType(models.Model): type = models.CharField(..)
I've found the answer if anyone will be looking for it in future.
Things like that are managed by using signals. For above example I've used pre_save signal in function decorated as reciver.
In my understanding, reciver catches data before or after saving in database - way of doing it is based on type of signal. Those functions could basicly do anything, for example - change type of Service based on given type_id.