我的领域:
signup_date = models.DateTimeField(blank=True,default=datetime.now)
保存时出错:
IntegrityError: null value in column "signup_date" violates not-null constraint
我正在尝试进行一个简单的单元测试,我从dict创建一个ModelForm的绑定实例并保存它。
感谢。
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/django/signupform/signup/insert_test_data.py", line 27, in <module>
SDF.save()
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/forms/models.py", line 371, in save
fail_message, commit, construct=False)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/forms/models.py", line 86, in save_instance
instance.save()
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/base.py", line 435, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/base.py", line 528, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/query.py", line 1479, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/sql/compiler.py", line 783, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/models/sql/compiler.py", line 727, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.5/site-packages/Django-1.2.1-py2.5.egg/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
return self.cursor.execute(query, args)
IntegrityError: null value in column "signup_date" violates not-null constraint
from django.db import models
from django.contrib.localflavor.us.models import PhoneNumberField
from datetime import datetime
from models_dropdowns import *
class SignupData(models.Model):
first_name = models.CharField(max_length=128,verbose_name='First Name')
last_name = models.CharField(max_length=128,verbose_name='Last Name')
street1 = models.CharField(max_length=128,verbose_name='Street Address 1')
street2 = models.CharField(max_length=128,verbose_name='Street Address 2')
city = models.CharField(max_length=128)
state = models.CharField(max_length=2)
zip = models.IntegerField(verbose_name='Zip Code')
phone_number = PhoneNumberField(verbose_name='Phone Number XXX-XXX-XXXX')
email = models.EmailField(verbose_name='Email Address')
contact_method = models.ForeignKey('ContactMethodChoice',blank=False,default=-1,verbose_name='Preferred contact method')
birth_date = models.DateField(verbose_name='Birth Date')
policy_number = models.CharField(max_length=128,verbose_name='American Family Auto Insurance Policy Number')
vin = models.CharField(max_length=128,verbose_name='Vehicle Identification Number (VIN)')
vehicle_make = models.ForeignKey('VehicleMakeChoice',verbose_name='VehicleMake')
vehicle_model = models.CharField(max_length=128,verbose_name='Vehicle Model')
vehicle_year = models.ForeignKey('VehicleYearChoice',verbose_name='Vehicle Year')
vehicle_ownership = models.ForeignKey('VehicleOwnershipChoice',blank=False,default=-1,verbose_name='Vehicle Ownership')
vehicle_use = models.ForeignKey('VehicleUseChoice',blank=False,default=-1,verbose_name='Use of Vehicle')
terms_and_conditions = models.BooleanField(verbose_name='I Agree to the terms and conditions. (add link)')
form_user_role = models.ForeignKey('FormUserRoleChoice',blank=False,default=-1,verbose_name='Your Role')
participate_in_feedback = models.BooleanField(verbose_name='<b>Opportunity to provide feedback.</b>...<br><br>',help_text='Please check the box if you would like to participate.')
signup_date = models.DateTimeField(blank=True,default=datetime.now,verbose_name='')
我使用以下代码创建表单的绑定实例并保存。我通过从./manage.py shell导入它来运行它。
from signupform.signup.forms import SignupDataForm
keys = ('first_name','last_name','street1','street2','city','state','zip','phone_number','email','contact_method','birth_date','policy_number','vin','vehicle_make','vehicle_model','vehicle_year','vehicle_ownership','vehicle_use','terms_and_conditions','form_user_role','participate_in_feedback')
data = [
('firstname1','lastname1','test1','test1','test1','XX',55555,'555-555-5555','test@asdf.com',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
('firstname2','lastname2','test2','test2','test2','XX',55555,'555-555-5555','test@asdf.com',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
('firstname3','lastname3','test3','test3','test3','XX',55555,'555-555-5555','test@asdf.com',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
('firstname4','lastname4','test4','test4','test4','XX',55555,'555-555-5555','test@asdf.com',1,'01/01/01','####-####-##-##-AAAA-AA','123456789abcdefgh',1,'model',1996,1,1,True,1,True),
]
for d in data:
tmpDict = {}
for i in range(0,len(keys)):
tmpDict[keys[i]] = d[i]
SDF = SignupDataForm(tmpDict)
if not SDF.is_valid():
print SDF.errors
else:
SDF.save()
答案 0 :(得分:2)
关于我的代码中的问题是你不应该写
default=datetime.now()
但是
default = datetime.now
将函数作为默认值传递,而不是在解析代码时传递函数的结果。目前,您的所有SignupData都具有相同的signup_date
那就是说,我不确定这是你的bug的原因。
答案 1 :(得分:0)
确保您实际上正在使用Django来创建对象。否则你将绕过所有Django魔法来设置默认值。
所以使用这样的东西:
SignupData.objects.create(blah)
要使用字典,请使用字典解包:
SignupData.objects.create(**my_dictionary)
顺便说一下,您可能希望默认参数是可调用的,即在now
函数之后省略parens。这样,每次创建新对象时都会调用该函数。对于parens,它只会被调用一次,并且相同的值将用于所有新创建的对象。