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(..)

Answers


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.


Need Your Help

Multiple database with Spring+Hibernate+JPA

java spring hibernate jpa multi-database

I'm trying to configure Spring+Hibernate+JPA for work with two databases (MySQL and MSSQL).

Why doesn't TextRenderer.MeasureText work properly?

c# .net winforms .net-4.0 textrenderer

I want to measure the height of the text given a certain width of available canvas. The text that I pass in is really long and I know will wrap. To that end, I call the following: