• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

views.py파일의 " instance=" 이라는 키워드를 사용하는 이유가 궁금합니다.

19.09.16 10:40 작성 조회수 92

0

본강좌 34Line에 기술하신

form = RestaurantForm(request.POST, instance=item)

여기서

1. request.POST는 초기화이고

2. instance=item 수정될 데이터가 item에 저장된 pk의 값이라는 것은 이해가 가는데, 그럴경우 pk=item이라고 해야 맞는것이 아닐까요?

3. 구글링을 조금더 해본결과 "수정시에는 instance=post 를 지정해줌으로서 기존에 있던 데이터를 불러올 수 있다."

라고 적혀있었습니다.

구글링 해본결과

As the documentation states, the "instance" keyword argument is passed the model whose relations that the formset will edit.

라고 기술되어있는데요.

도대체 instance라는 변수가 어느것의 property이고 어떤역활을 하는지 궁금합니다.

pk키워드는 앞에있는 ORM 메서드를 사용하였을경우 사용하는 것으로 보이며

instance키워드는 RestaurantForm이라는 객체를 초기화할 경우 사용되는 것으로 보입니다만.

개념이 에메모호하여 정확한 설명 부탁드립니다.

===============================================

Instances

Not unlike the ambiguity between "class" and "type", "instance" is synonymous to "object". Think of it this way: objects are instances of types. So, "42 is an instance of the type <tt class="docutils literal" style="box-sizing:border-box;font-family:Consolas, monaco, monospace;color:#000000;background-color:#f7f7f7;white-space:nowrap;border-radius:2px;font-size:14.4px;padding:2px">int</tt>" is equivalent to "42 is an <tt class="docutils literal" style="box-sizing:border-box;font-family:Consolas, monaco, monospace;color:#000000;background-color:#f7f7f7;white-space:nowrap;border-radius:2px;font-size:14.4px;padding:2px">int</tt> object". I usually use "instance" and "object" interchangeably. In some cases when I want to specifically refer to objects as artifacts of the CPython implementation, I will try to use "instance" to refer to actual instances of classes. Another place where the term "instance" is explicitly used by Python is in built-ins like <tt class="docutils literal" style="box-sizing:border-box;font-family:Consolas, monaco, monospace;color:#000000;background-color:#f7f7f7;white-space:nowrap;border-radius:2px;font-size:14.4px;padding:2px">isinstance</tt> and the special <tt class="docutils literal" style="box-sizing:border-box;font-family:Consolas, monaco, monospace;color:#000000;background-color:#f7f7f7;white-space:nowrap;border-radius:2px;font-size:14.4px;padding:2px">__instancecheck__</tt> attribute.

===============================================

다른언어를 사용하던 개발자로서 instance= 해당 코드에서 어떤역활을 하는지 알수가 없어 답답함에 여쭤봅니다.

답변 1

답변을 작성해보세요.

0

답변이 늦어 죄송합니다.

일단 말씀하신대로 초기화 시 파라미터들을 업데이트 하기 위해 request.POST를 첫번째 인자로 넘기는 건 말씀하신 내용이 맞습니다. 이 때는 initial 이라는 ModelForm의 property의 역할을 합니다. 

ModelForm의 instance의 경우

instance는 모델폼 객체가 생성될 때 실제 db의 값을 전달할 때 사용합니다. (즉, 이 데이터와 1:1 대응되는 다른 객체(instance)를 집어넣게 됩니다.)

말씀하신대로 같은 pk(primary key)를 가진 디비의 레코드와 생성할 모델 폼을 매핑시킨다고 생각하시면 되는데 다만 모델 폼에서는 instance=item이 pk=item.id 같은 형태로 생각하시면 이해하기 좋으실 거 같습니다. 

여기서 pk 인자 값은 ORM에서 데이터를 불러올 때 사용되고 instance 인자 값은 ModelForm에 ORM객체를 매핑시킬 때 사용됩니다.

--

간단한 로직을 설명하면,

instance는 ModelForm의 생성자 실행 시에 객체 생성의 인자 값으로 들어갑니다. (method의 arguments를 라벨링해서 들어갑니다.)

그러면서 pk값은 instance 인자로 받은 실제 item의 pk로 매핑되고 기존 디비의 데이터들로 채워지게 되는데요. 이 때 추가적으로 request.POST 값을 initial 로 넘기게 되면 그 레코드의 값들이 request.POST에서 받은 값들로 덮어씌어지게 됩니다.