views.py
@login_required
def become_vendor(request):
vendordetailform = VendorAdminDetailsForm()
if request.method == 'POST':
vendordetailform = VendorAdminDetailsForm(request.POST, request.FILES)
if vendordetailform.is_valid():
vendordetailform.instance.vendoruser = request.user
request.user=vendordetailform.save()
request.user.is_active=False
request.user.save()
user_details = CustomUser.objects.filter(id=request.user)
vendor_details = user_details[0].vendor_details.all()
return render(request,'vendor/preview.html', {'user_details':user_details, 'vendor_details':vendor_details})
else:
vendordetailform = VendorAdminDetailsForm()
return render(request, 'vendor/become_vendor.html', {'vendordetailform':vendordetailform})
models.py
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
mobile_number = models.IntegerField(blank=False, null=True)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
customer = models.BooleanField(default=False)
vendor = models.BooleanField(default=False)
# is_approved = models.BooleanField(default=True)
id = models.AutoField(primary_key=True, editable=True)
userid= models.CharField(max_length=100, unique=True, null=True)
objects = UserManager()
def __str__(self):
return str(self.email) '-' self.userid
class VendorDetails(models.Model):
vendoruser = models.ForeignKey(CustomUser, null=True,on_delete=models.CASCADE, related_name='vendor_details')
type_of_vendor = models.CharField(max_length=200)
aadhar_number = models.CharField(max_length=200)
aadhar_image = models.ImageField(upload_to='Images')
pan_number = models.CharField(max_length=200)
pan_image = models.ImageField(upload_to='Images')
store_name = models.CharField(max_length=200)
brand_name = models.CharField(max_length=200)
mail_id = models.EmailField(max_length=200)
contact_no = models.IntegerField(blank=True, null=True)
gst_number = models.CharField(max_length=100)
attach_gst_cert = models.ImageField(upload_to='Images')
acct_number = models.CharField(max_length=100)
attach_passbook = models.ImageField(upload_to='Images')
ifsc_code = models.CharField(max_length=100)
fb_account = models.CharField(max_length=100,blank=True, null=True)
insta_account = models.CharField(max_length=100,blank=True, null=True)
website = models.CharField(max_length=100,blank=True, null=True)
door_no = models.CharField(max_length=100,blank=True, null=True)
street_name = models.CharField(max_length=100, blank=True, null=True)
city = models.CharField(max_length=100, blank=True, null=True)
pincode = models.IntegerField(blank=True, null=True)
def __str__(self):
return str(self.id) " - " str(self.vendoruser)
I have two models 1. User model, 2. VendorDetails model (vendoruser is connected by foreignkey relationship with vendordetails)
Here I save the User extra details (VendorDetailsForm) if the requested user is applying for a vendor. I could be saved the requested user details in VendorDetails Model. But when I am getting the requested user id, I can't . instead I get vendordetails model foreignkey field. Here I want to inactive the requested user who came with this vendor details form. I am getting Field 'id' expected a number but got <VendorDetails: [email protected]>.
CodePudding user response:
your problem is that you are assigning request.user
to vendordetailform.save()
that is not correct remove that line and pass request.user.id
to CustomUser
Try something like this.
@login_required
def become_vendor(request):
vendordetailform = VendorAdminDetailsForm()
if request.method == 'POST':
vendordetailform = VendorAdminDetailsForm(request.POST, request.FILES)
if vendordetailform.is_valid():
vendordetailform.instance.vendoruser = request.user
vendordetailform.save()
request.user.is_active=False
request.user.save()
user_details = CustomUser.objects.filter(id=request.user.id)
vendor_details = user_details[0].vendordetails_set.all()
return render(request,'vendor/preview.html', {'user_details':user_details, 'vendor_details':vendor_details})
else:
vendordetailform = VendorAdminDetailsForm()
return render(request, 'vendor/become_vendor.html', {'vendordetailform':vendordetailform})
CodePudding user response:
Try this instead : user_details = CustomUser.objects.filter(id=request.user.id)
if vendordetailform.is_valid():
# Some code ...
user_details = CustomUser.objects.filter(id=request.user.id)
# Some code ...