Python 使用django createview创建相关对象

Python 使用django createview创建相关对象,python,django,Python,Django,嘿,我正在使用Django 1.10.7 我正在创建一个表单,该表单将创建一个商店位置。现在我想做的是,在加载表单时,让存储已经与存储位置相关联 url具有商店外键的正确id,例如:localhost:8000//office/dealer/1/location/create。我看到store键在request关键字参数中。但我不知道如何将其关联到表单中。任何帮助都将不胜感激 下面是我的代码 #views.py class DealerLocationCreateView(CreateView

嘿,我正在使用Django 1.10.7

我正在创建一个表单,该表单将创建一个商店位置。现在我想做的是,在加载表单时,让存储已经与存储位置相关联

url具有商店外键的正确id,例如:localhost:8000//office/dealer/1/location/create。我看到store键在request关键字参数中。但我不知道如何将其关联到表单中。任何帮助都将不胜感激

下面是我的代码

#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)
-

#url.py
url(r'^dealer/(?P\d+)/location/create',DealerLocationCreateView.as_view(),name='new-location'),
为什么不使用:

def form_valid(self, form):
    pk = self.kwargs.get("dealer_pk", None)
    form.instance.dealer = pk
    return super(DealerLocationCreateView, self).form_valid(form)
为什么不使用:

def form_valid(self, form):
    pk = self.kwargs.get("dealer_pk", None)
    form.instance.dealer = pk
    return super(DealerLocationCreateView, self).form_valid(form)