0

I'm running into a stange issue in Django and I think it might be because I don't properly understand how class based views work.

Basically I have a ListView and a FormView. one is to list the objects, the other is to edit.

In a browser, if i navigate from listview to the formview of an existing object I get the expected result: the form fields are filled in with expected values.

The same formview is used for presenting a blank form where a user can create objects.
If i navigate from the form of an existing object to the formview of a new object or to the listview and then to the formview of a new object, initial (i.e. self.initial of the CBV) is prepopulated with information from the existing object previously visited.
Obviously I'm expecting self.initial to be blank at the beginning of a new get request.

My understanding was that each request generates a new instance of a classbased view. How is initial getting carried over across requests? I'm coming to this conclusion based on some debugging. relevant lines in get_initial() below.

def get_initial(self, **kwargs):
    initial = super(M_EditNewsletterView, self).get_initial(**kwargs)       
    fs_logger.debug('initial immediately after super -> %s' % initial)

SO suggested this was similar, but I don't understand the answer with the upvote. Django(trunk) and class based generic views: one form's initial data appearing in another one's

can anyone help explain to me what is going on?

Community
  • 1
  • 1
w--
  • 6,427
  • 12
  • 54
  • 92

1 Answers1

1

get_initial will simply return the value based on the initial property for the class. Just a guess - but are you setting initial like this?

I.e.

class M_EditNewsletterView(FormView):
    #this value will essentially be constant as it belongs to the 
    #class, not the instance
    initial = get_my_value()

You should override the 'get_initial' method yourself.

def get_initial(self, **kwargs):
    return get_my_value()
David Neale
  • 16,498
  • 6
  • 59
  • 85
  • Thanks for the reply David. Unfortunately no. I am overriding get_initial.... the current hack to fix the problem is in fact to set the first line in get_initial() as initial = {} – w-- Jun 13 '12 at 18:28