Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 外键自动填充表单_Django_Django Forms_Django Views_Django Haystack - Fatal编程技术网

Django 外键自动填充表单

Django 外键自动填充表单,django,django-forms,django-views,django-haystack,Django,Django Forms,Django Views,Django Haystack,我有四个模型的商店,客户,产品,订单。 我正在展示模型之间的关系 商店 顾客 产品 秩序 当客户登录时,商店将在屏幕上打印,并在商店上显示一个按钮,以在表单卡中显示商店的产品 我如何创建订单,以便订单中的客户是实例,订单中的店铺是选择用来显示产品的店铺,并且产品的每张卡片都有一个字段来填写剩余细节并提交如果我是您,我将使用以下两个选项之一将其添加到表单中 选择1 选择2 # If the form is already created you can use this pattern conte

我有四个模型的商店,客户,产品,订单。 我正在展示模型之间的关系

商店 顾客 产品 秩序 当客户登录时,商店将在屏幕上打印,并在商店上显示一个按钮,以在表单卡中显示商店的产品


我如何创建订单,以便订单中的客户是实例,订单中的店铺是选择用来显示产品的店铺,并且产品的每张卡片都有一个字段来填写剩余细节并提交

如果我是您,我将使用以下两个选项之一将其添加到表单中 选择1

选择2

# If the form is already created you can use this pattern
context['form'].fields['shop'].initial = shop
context['form'].fields['customer'].initial = self.request.user

你做得怎么样?我很困惑,你可以向我推荐一个主意。如果我在产品卡上点击“添加”按钮,并在提交后,添加产品的列表将打印剩余信息字段,如数量等,以填充它们
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE
    shop = models.ForeignKey(Shop, models.CASCADE, null=True, blank=True)
shop = models.ForeignKey(Shop, models.CASCADE, null=True)
customer = models.ForeignKey(Customer, models.CASCADE, null=True)
product = models.ForeignKey(Product, models.CASCADE, null=True)
def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    # Get the shop_id from the URL
    shop = get_object_or_404(Shop, pk=shop_id)
    # Create a form and add it the context
    form = OrderForm(
        # This is where the magic happens
        initial={"shop": shop], "customer": self.request.user})
    # Use this if you want to hide it as well
    form.fields['item'].widget = forms.HiddenInput()
# If the form is already created you can use this pattern
context['form'].fields['shop'].initial = shop
context['form'].fields['customer'].initial = self.request.user