python - Default instance for Django model -


my questionis: there anyway create default instance class language? :

class language(models.      default_instance = language(name="unknown")      name = models.charfield(max_length=100) 

so class student can use this:

class student(models.model):          name = models.charfield(max_length=50)          language = models.foreignkey(language, default = language.default_instance.pk ) 

thank you!

you set overriding save method:

#your model class student(models.model):   name = models.charfield(max_length=50)   language = models.foreignkey(language)      # override save     def save(self,**args,**kwargs):       if self.language none:         default_language = language.objects.get(pk=1) #set default         self.language = default_language     super(student,self).save(self,*args,**kwargs) # 'real' save 

see here overriding save method.


Comments

Popular posts from this blog

mongodb - How to keep track of users making Stripe Payments -

javascript - Fire on return from form "Submit" -

javascript - Thinglink image not visible until browser resize -