model of contact
class Contact(models.Model):
owner = models.ForeignKey(User,related_name="contacts",on_delete=models.CASCADE)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
account_name = models.CharField(max_length=100)
title = models.CharField(max_length=200,null=True,blank=True)
email = models.EmailField(null=True,blank=True)
phone = models.BigIntegerField(null=True,blank=True)
mobile = models.BigIntegerField(null=True,blank=True)
assistant = models.CharField(max_length=100,null=True,blank=True)
assistant_phone = models.BigIntegerField(null=True,blank=True)
Department = models.CharField(max_length=100,null=True,blank=True)
fax = models.CharField(max_length=100,null=True,blank=True)
date_of_birth = models.DateField(null=True,blank=True)
skype_id = models.CharField(max_length=100,null=True,blank=True)
twitter = models.CharField(max_length=100,null=True,blank=True)
country = models.CharField(max_length=100,null=True,blank=True)
state = models.CharField(max_length=100,null=True,blank=True)
city = models.CharField(max_length=100,null=True,blank=True)
street = models.CharField(max_length=100,null=True,blank=True)
pin_code = models.IntegerField(null=True,blank=True)
description = models.TextField(null=True,blank=True)
added_on = models.DateTimeField(auto_now_add=True)
As you can some fields have blank=True
means we don't need to give a value to that field during creating a new object
views.py for saving new objects of contact model.
def save_contact(request):
if request.method == "POST":
first_name = request.POST.get("fname")
last_name = request.POST.get("lname")
contact_name = request.POST.get("contact_name")
title = request.POST.get("title")
email = request.POST.get("email")
phone = request.POST.get("phone")
mobile = request.POST.get("mobile")
assistant = request.POST.get("assistant")
assistant_phone = request.POST.get("assistant_phone")
department = request.POST.get("department")
fax = request.POST.get("fax")
date_of_birth = request.POST.get("date_of_birth")
skype_id = request.POST.get("skype_id")
twitter = request.POST.get("twitter_id")
country = check(request.POST.get("country")
state = request.POST.get("state")
city = request.POST.get("city")
street = request.POST.get("street")
pin_code = request.POST.get("pin_code")
description = request.POST.get("description")
contact_obj = Contact(owner=request.user,first_name=first_name,last_name=last_name,account_name=contact_name,title=title,email=email,phone=phone,mobile=mobile,assistant=assistant,assistant_phone=assistant_phone,Department=department,fax=fax,date_of_birth=date_of_birth,skype_id=skype_id,twitter=twitter,country=country,state=state,city=city,street=street,pin_code=pin_code,description=description)
try:
Contact.save(self=contact_obj)
except:
messages.success(request,"Something went wrong! Try again")
return redirect("crm_contacts")
messages.success(request,"Your contact has been successfully saved")
return redirect("crm_contacts")
else:
messages.success(request,"Something went wrong! Try again")
return redirect("crm_contacts")
Now the problem is that I need to give all field values in the new contact object. let's suppose if the user does not give any value of the phone field then I'm able to store it in the database I got an error during save the object. is there any way that I will give only those fields to contact object that has a value which is given by user so contact object can be store in the database.
Error = ValueError: invalid literal for int() with base 10: ''
I think you should check out the model forms in Django. That way it will be more cleaner and the validations are inbuilt django model forms
with forms, you can create a form object and save them later
new_author = f.save(commit=False)
new_author.property = value
new_author.save()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.