【问题标题】:How to save object of django model without giving all fields values?如何在不提供所有字段值的情况下保存 django 模型的对象?
【发布时间】:2020-10-27 11:26:01
【问题描述】:

联系方式

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: ''

【问题讨论】:

  • 当您使用 pin_code = request.POST.get("pin_code") 时,它将有一个空字符串而不是 None,因为用户发布了一个空值。当您尝试保存模型时,所有 int 字段都会出错。在您的情况下,您需要手动检查给定值是否为空字符串,那么它应该为无。但是你最好ModelForm 它会为你做所有检查
  • @AndreyMaslov 谢谢我检查 if-else 并没有给空字符串和工作,但是当我看到 HTML 格式的新联系人对象时,它在 HTML 中没有显示。我认为模型形式最好我会使用它。
  • 您可以在创建模型对象时添加检查 Contact(..., pin_code = pin_code if pin_code else None, ...)ModelForm 会是更好的决定

标签: python django django-models django-views


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多