models.py
class CustomUser(AbstractUser):
username = None
email = models.EmailField(_('email address'), unique=True)
mobileno = models.IntegerField(blank=True, null=True)
is_customer = models.BooleanField(default=False)
is_vendor = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager()
def __str__(self):
return self.email
class VendorDetails(models.Model):
vendoruser = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
aadhar_number = models.CharField(max_length=200)
pan_number = models.CharField(max_length=200)
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)
acct_number = models.CharField(max_length=100)
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 self.mail_id
forms.py
class UserCreationForm(UserCreationForm):
"""
A Custom form for creating new users.
"""
class Meta:
model = CustomUser
fields = ['email','first_name','last_name','mobileno']
""" For Vendor users forms"""
class VendorAdminDetailsForm(forms.ModelForm):
class Meta:
model = VendorDetails
fields = ['aadhar_number', 'pan_number']
class VendorStoreDetailsForm(forms.ModelForm):
class Meta:
model = VendorDetails
fields = ['store_name', 'brand_name', 'mail_id', 'contact_no', 'gst_number']
class VendorBankDetailsForm(forms.ModelForm):
class Meta:
model = VendorDetails
fields = ['acct_number', 'ifsc_code']
class VendorSocialMediaDetailsForm(forms.ModelForm):
fb_account = forms.CharField(required=False)
insta_account = forms.CharField(required=False)
website = forms.CharField(required=False)
class Meta:
model = VendorDetails
fields = ['fb_account', 'insta_account', 'website']
class VendorContactDetailsForm(forms.ModelForm):
door_no = forms.CharField(required=False)
street_name = forms.CharField(required=False)
pincode = forms.IntegerField(required=False)
class Meta:
model = VendorDetails
fields = ['door_no', 'street_name', 'city', 'pincode']
views.py
#Vendor Signup
def VendorSignup(request):
form = UserCreationForm()
vendordetail = VendorAdminDetailsForm()
vendorstore = VendorStoreDetailsForm()
vendorbankdetail = VendorBankDetailsForm()
vendorsocialmedia = VendorSocialMediaDetailsForm()
vendorcontact = VendorContactDetailsForm()
if request.method == 'POST':
form = UserCreationForm(request.POST)
vendordetail = VendorAdminDetailsForm(request.POST)
vendorstore = VendorStoreDetailsForm(request.POST)
vendorbankdetail = VendorBankDetailsForm(request.POST)
vendorsocialmedia = VendorSocialMediaDetailsForm(request.POST)
vendorcontact = VendorContactDetailsForm(request.POST)
if form.is_valid() and vendordetail.is_valid() and vendorstore.is_valid() and vendorbankdetail.is_valid() and vendorsocialmedia.is_valid() and vendorcontact.is_valid():
form.save()
print("form")
vendordetail.save()
vendorstore.save()
vendorbankdetail.save()
vendorsocialmedia.save()
vendorcontact.save()
return redirect('login')
else:
form = UserCreationForm()
vendordetail = VendorAdminDetailsForm()
vendorstore = VendorStoreDetailsForm()
vendorbankdetail = VendorBankDetailsForm()
vendorsocialmedia = VendorSocialMediaDetailsForm()
vendorcontact = VendorContactDetailsForm()
I have created multiple forms for signup with extending abstractuser model. also I have put all these forms in template separately. Is it the correct way to handle multiple forms. Facing issue in the line (vendordetail.save()) Not null field (onetoonefield related issue). But I don't know how to write views for this all forms. Please help me to get this solved.
CodePudding user response:
Where are you inserting the correct vendoruser
? That needs to be filled in prior to calling save().
You need to do something like:
vendordetail.instance.vendoruser = request.user
If indeed vendoruser
is the user that's currently logged in, else you need to get it from somewhere else. After insertion you can call:
vendordetail.save()
Edit: I missed that you are creating your user on the same page in another form. In that case you can first save your user form:
new_user = form.save()
The save() method of a model form will return the new object, therefore you can use it afterwards for your other forms:
vendordetail.instance.vendoruser = new_user