如何填充django表单,基于两个ModelForm

时间:2018-01-27 19:23:03

标签: django python-3.x

enter image description here我希望我的应用用户能够使用一个html页面编辑两个表单,这两个表单是否会填充来自两个相关模型的初始数据,但我还无法完成此结果,

models.py

class User(AbstractUser):
    is_vendor = models.BooleanField(default=False)

class Vendor(models.Model):
  user = models.OneToOneField(User)
  phone = models.CharField(max_length=15)

forms.py

class VendorProfileForm(forms.ModelForm):
    class Meta:
        model = Vendor
        fields = ['phone',]


class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['username','email']

views.py

def Edit_Vendor_Profile(request, pk):
    # querying the custom User model .
    user = User.objects.get(pk=pk)
    vendor = Vendor.objects.get(user_id=user.id)

    if request.method == "POST":
        vendor_form = VendorProfileForm(request.POST,request.FILES,                                                    
                                                     instance=vendor)
        user_form = UserForm(request.POST, instance=user)

        if vendor_form.is_valid() and user_form.is_valid():
            vendor_form.save()
            user_form.save()

            return HttpResponseRedirect('/profile/')
    else:
        vendor_form = VendorProfileForm(request.POST,request.FILES,                                                 
                                                     instance=vendor)
        user_form = UserForm(instance=user)

        return render(request, "accounts/update.html", {'vendor_form':vendor_form, 'user_form':user_form})

所以问题是用户模型的字段正在填充,但供应商模型的字段不是。所以我做错了, 非常感谢你的进步。

根据LeLouch先生的回答,我编辑了我的views.py,女巫非常接近,但仍然无法正确填写所有表格字段。

注意:我有一个图像字段,其数据正确填充!!

2 个答案:

答案 0 :(得分:0)

尝试:

def Edit_Vendor_Profile(request, pk):
    # querying the custom User model .
    user = User.objects.get(pk=pk)

    if request.method == "POST":
        vendor_form = VendorProfileForm(request.POST,request.FILES, 
                                                        instance=user.vendor)
        user_form = UserForm(request.POST, instance=user)

        if vendor_form.is_valid() and user_form.is_valid():
            vendor_form.save()
            user_form.save()

            return HttpResponseRedirect('/profile/')
    else:
        vendor_form = VendorProfileForm(instance=user.vendor)
        user_form = UserForm(instance=user)

        return render(request, "accounts/update.html", {'vendor_form':vendor_form, 'user_form':user_form})

答案 1 :(得分:0)

class Vendor(models.Model):
  user = models.OneToOneField(User)
  phone = models.CharField(max_length=15)


#using this to create a vendor model every time a user is created
def create_vendor(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        vendor = Vendor(user=user)
        vendor.save()
post_save.connect(vendor, sender=User)


def Edit_Vendor_Profile(request, pk):
        # querying the custom User model .
        user = User.objects.get(pk=pk)
        vendor = Vendor.objects.get(id= user.id)
        user_form = UserForm(instance=user)
        vndor_form = Vendor(instance=vendor)
        if request.method == "POST":
            vendor_form = VendorProfileForm(request.POST,request.FILES, 
                                                            instance=vendor)
            user_form = UserForm(request.POST, instance=user)

            if user_form.is_valid():
                created_user = user.save() (commit=False)
                vendor_form = VendorProfileForm(request.POST,request.FILES, 
                                                            instance=created_user)
                if vendor_form.is_valid():
                    created_user.save()
                    vendor_form.save()

                return HttpResponseRedirect('/profile/')
        else:
            vendor_form = VendorProfileForm()
            user_form = UserForm()

            return render(request, "accounts/update.html", {'vendor_form':vendor_form, 'user_form':user_form})