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
Post a Comment