"Column 'created_date_time' cannot be null" Django -


views.py

def when(request):      if request.method == 'post':          reportform = reportform(data=request.post)          if reportform.is_valid():             log.debug("test:%s",reportform)             report = reportform.save(commit=false)             report.user = request.user             report.save() 

models.py is

class report(models.model):     user = models.foreignkey(user, null=false)     incident_number = models.charfield('incident number', max_length=100)     device_id = models.charfield('device id', max_length=100)     app_uuid = models.charfield('unique app id', max_length=100)     created_date_time = models.datetimefield('created')     manual_date_time = models.datetimefield('another time', null=true, blank=true)     sent_date_time = models.datetimefield('sent') 

in above model,manual_date_time manually enter user.but created_date_time , send_date_time should generated django since function not yet implemented in app,django giving following error"(1048, "column 'created_date_time' cannot null")".

how hardcode using form , sent value can enter date , time manually.want pass hardcoded value both created_date_time , `send_date_time using form.how solve error.

well, can several things fix this:

first, can add attribute auto_now=true django add automatically now() date when object created.

created_date_time = models.datetimefield('created', auto_now=true) 

you can add default value datetimefield this:

created_date_time = models.datetimefield('created', default=datetime.now) 

and can add null=true has manual_date_time add db null value.

also, may add date manually when create object this:

    datetime import datetime     if reportform.is_valid():         log.debug("test:%s",reportform)         report = reportform.save(commit=false)         report.user = request.user         report.created_date_time = datetime.now()         report.save() 

the same goes sent_date_time field.

hope helps.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -