2010-02-26 8 views
1

, 나는 이것을 달성 할 장고 모델 매개 변수 :추가 장고 모델

class Foo(models.Model): 
    name = models.CharField(max_length=50) 

    #wrapping the save function, including extra tasks 
    def save(self, *args, **kwargs): 
     super(Foo, self).save(*args, **kwargs) 

      if extra_param: 
      ...do task 1 
     else: 
      ...do task 2 

그리고 나는이 작업을 수행 할 수있는 방법

Foo(name="Bill Gates",extra_param=True).save() # now triggers the task 1 
Foo(name="Bill Gates").save() # now triggers the task 2 

등 전달하려는 푸 레이팅 동안? 또한

감사

답변

9

당신은 당신의 모델이 아닌 지속 필드를 정의 할 수 있습니다 어떤 다른 제안 : 열려입니다.

또는
class Foo(models.Model): 
    name = models.CharField(max_length=50) 
    extra_param = False 

def save(self, *args, **kwargs): 
    ... 
    print self.extra_param 

, 당신은 할 수 있습니다 :

Foo(name="Bill Gates").save(extra_param=True) 

def save(self, *args, **kwargs): 
    ... 
    print kwargs["extra_param"]