Static/constant dictionary for get_initial or get_form_kwargs

24 views
Skip to first unread message

Bob Glassett

unread,
Feb 27, 2018, 8:10:55 PM2/27/18
to Django users
Hello, 

Trying to bring a django app into production, and I ran into a real headscratcher.

I have a Class based view inherited from create.  When the page reloads, after validation, I check the initial dictionary for a field's value to fill in the queryset for a different field.

I'm getting sporadic errors about one of the fields not found in the initial object.  When I looked closer into the problem, the initial dicitonary matched a form for a totally different model.

If I need to pre-populate the initial dictionary, I override the get_initial and return the dictionary that I want.  I am not setting initial= in the class definition.  Is this the right way to do this task?

I am concerned about a static initial dictionary sticking around.  The base edit class returns a copy of the initial dictionary, but if the initial dicitonary somehow has invalid values in it, I could be seeing this for all my forms.

This is what I did for the initial dictionary:

class UserCreateView(AdminCreateView):

    model=User

    success_url='/portal/accounts/list'

    form_class=PortalUserForm 

    

    def get_form_kwargs(self):

        kwargs = super(UserCreateView, self).get_form_kwargs()

        kwargs.update({'request' : self.request})

        return kwargs 

    

    def get_initial(self):

        return {}


On a ModelForm (unrelated to the form/model above) I was trying to access the self.initial for a particular field, which threw a Key exception.  The initial dictionary passed down on the yellow screen did not match the form or data for the view at all.


kwargs:  



{'initial': {'agency': <Agency: Baldwin>, 'canEnterMealCounts': False, 'canManageCalendar': True, 'canManageCustomerAllergies': True, 'canManageFieldTrips': True, 'canManageSocializations': True, 'canManageSpecialOrders': True, 'canManageSupplies': False, 'canPrintMenus': True, 'chefablesUser': False, 'location': <QuerySet []>, 'phone': PhoneNumber(country_code=1, national_number=2018158136, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=1, preferred_domestic_carrier_code=None)}, 'instance': None, 'prefix': None}


I have no idea where that initial dictionary came from.  My get_form_kwargs looks like this:


    def get_form_kwargs(self):

        kwargs = super(PendingLabelsCreateView, self).get_form_kwargs()

        kwargs.update({'user': self.request.user})

        return kwargs


The direct ancestor doesn;'t have get_form_kwargs defined, and that is defined as such:


class AdminCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):


I need to understand where that initial value came from and determine if I have static values where I don't want them.


Thanks in advance



Matemática A3K

unread,
Feb 28, 2018, 11:29:38 PM2/28/18
to django...@googlegroups.com
Maybe they are set in the Form?
 

Thanks in advance



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/db24def3-f8cc-4954-bbd3-72b6ed3fa0d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages