首页 > 解决方案 > 如何在不提供所有字段值的情况下保存 django 模型的对象?

问题描述

接触模型

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意味着我们在创建新对象期间不需要为该字段赋值

views.py 用于保存接触模型的新对象。

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")

现在的问题是我需要在新的联系人对象中提供所有字段值。假设如果用户没有给出电话字段的任何值,那么我可以将其存储在数据库中,但在保存对象期间出现错误。有什么方法可以只将那些字段提供给具有用户给出的值的联系人对象,以便可以将联系人对象存储在数据库中。

错误=ValueError: invalid literal for int() with base 10: ''

标签: pythondjangodjango-modelsdjango-views

解决方案


我认为您应该查看 Django 中的模型表单。这样它会更干净,并且验证是内置的 django 模型表单

使用表单,您可以创建表单对象并稍后保存

new_author = f.save(commit=False)
new_author.property = value
new_author.save()

推荐阅读