[英]How to save object of django model without giving all fields values?
model 触点
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)
正如您所见,某些字段有blank=True
意味着我们在创建新的 object 期间不需要为该字段提供值
views.py 用于保存联系人 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")
现在的问题是我需要在新联系人 object 中给出所有字段值。 假设如果用户没有给出电话字段的任何值,那么我可以将其存储在数据库中。我在保存 object 期间出现错误。 有什么方法可以让我只提供那些联系 object 的字段,其值由用户提供,因此联系 object 可以存储在数据库中。
Error = ValueError: invalid literal for int() with base 10: ''
我认为您应该查看 Django 中的 model forms。 这样它会更 干净,并且验证是内置的
使用 forms,您可以创建一个表格 object 并稍后保存
new_author = f.save(commit=False)
new_author.property = value
new_author.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.