Reputation: 81
Hey All I am using Django 1.10.7
I am creating a form that creates a store location to a store. Now what I want to happen is to have the store already associated with the store location when the form is loaded.
The url has the proper id of the foreign key of the store ex: localhost:8000//office/dealer/1/location/create. and I see that that the store key is in the request keyword arguments. But I can't get how to associate that into the form. Any help would be really appreciated
Here is how I have my code
#views.py
class DealerLocationCreateView(CreateView):
model = models.DealerLocation
fields = ['dealer'
'dealer_location_name',
'dealer_location_mailing_address',
'dealer_location_mailing_city',
'dealer_location_mailing_state',
'dealer_location_mailing_zipcode',
'dealer_location_mailing_phone',
'dealer_location_shipping_address',
'dealer_location_shipping_city',
'dealer_location_shipping_state',
'dealer_location_shipping_zipcode',
'dealer_location_shipping_phone'
]
def form_valid(self, form):
form.instance.dealer = self.request.dealer_pk
return super(DealerLocationCreateView, self).form_valid(form)
-
# urls.py
url(r'^dealer/(?P<dealer_pk>\d+)/location/create', DealerLocationCreateView.as_view(), name='new-location'),
Upvotes: 0
Views: 888
Reputation: 1118
Why not using :
def form_valid(self, form):
pk = self.kwargs.get("dealer_pk", None)
form.instance.dealer = pk
return super(DealerLocationCreateView, self).form_valid(form)
Upvotes: 2