视图.py
def become_vendor(request):
# vendorform = VendorCreationForm()
vendordetailform = VendorAdminDetailsForm()
if request.method == 'POST':
# vendorform = VendorCreationForm(request.POST)
vendordetailform = VendorAdminDetailsForm(request.POST, request.FILES)
if vendordetailform.is_valid():
# if vendorform.is_valid():
# new_user = vendorform.save()
print("hello")
vendordetailform.instance.vendoruser = request.user
print("hello1")
request.user=vendordetailform.save()
print("hello2")
request.user.is_active = False
request.user.save()
user_details = CustomUser.objects.filter(id=request.user.id)
vendor_details = user_details[0].vendor_details.all()
return render(request,'vendor/preview.html', {'user_details':user_details, 'vendor_details':vendor_details})
else:
# vendorform = VendorCreationForm()
vendordetailform = VendorAdminDetailsForm()
在这里,我在用户登录后将用户存储详细信息保存在外键相关模型中。在这里,我遇到了问题vendordetailform.instance.vendoruser = request.user
。用户实例未存储,并且 user.is_active=False 没有发生。错误是Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x00000158FEFB15B0>>": "VendorDetails.vendoruser" must be a "CustomUser" instance.