Default value for field set to current datetime in form view in Odoo - field

I have a field last_update in which I want to store the current datetime as a user edits a form in form view. Basically I need to default the value of last_update to the system's date.
I tried using:
<field name="last_update" default_last_update="datetime.now()"/>
But, it is not working.

In the python file:
from datetime import datetime
last_update = fields.Datetime(string='Last Update',default=lambda self: fields.datetime.now())

In Every model, you will always have "write_date", which stores last record update time.
Still if you wants to add this field and update it every time when record update then you can set default when record create and inherit write() method to update every time when record update, set current time in that field as following :
last_update = fields.Datetime(string='Last Update',default=fields.Datetime.now)
#api.multi
def write(self, vals):
vals.update({'last_update':fields.Datetime.now})
return super(<your_class_name>, self).write(vals)

You Can Also Use This:
from datetime import datetime
last_update = fields.Date(string='Last Update',default=datetime.now())

Related

ASP.Net MVC DateTimePicker inside Kendo Gird not getting current time on Create

I have a popup with kendo grid inside that displays records, the first column is
col.Bound(m => m.RecordDate)
.EditorTemplateName("DateTime")
.Width(180).Format("{0:dd/MM/yyyy h:mm tt}");`
This is the DateTime in EditorTemplate -
#(Html.Kendo().DateTimePickerFor(m => m)
.HtmlAttributes(new { data_bind = dataBind}))`
The problem is when I go to that view, and create a record let's say current datetime is 19/10/2017 8:30 AM when i save the record it get's the correct time, the grid is reloaded but the popup is not being closed and then let's say 3 minutes have passed, and I create a record again but the datetimepicker's default value is still 19/10/2017 8:30 AM, instead of 19/10/2017 8:33 AM
Since the window is not being closed then the binded dateTime to the datePicker will allways the be the dateTime that was binded the first time the window was loaded. The element doesn't refresh and there isn't any configuration for it to always get the current DateTime since it is not possible.
If for example you open any window and until your save a couple of minutes have passed the value of the dateTime will again be wrong. If you want the absolute moment of the save you can't give it to the user to insert it or alter it. You should set it at the save event at that exact moment that the save occurs
If however you still want to allow the user to handle that dateTime one way I can think of is to reset the dateTimePicker value to Date.now() every time the user focuses to the window. Obviously you will still have the issue of the time passing while the user inserts his data...

One form input to set two database values

I have a form which when submitted creates a new record in a database. I have two values in the database, value_1 & value_2. In the form there is an input field for value_1 which is a dropdown value of yes and no. When the form is submitted I wish to have value_1 and value_2 set to the value selected in the input field for value_1. So if dropdown for value_1 is set to yes then value_2 is also set to yes.
I am currently using the following but believe there must be a more elegant solution:
params[:person][:free] = params[:person][:trial]
#person = Person.new(params[:person])
#person.update_attribute(:free, params[:person][:free])
First you should think about what you really want. What is your logic.
Do you want value_2 always equal to value_1? I hope not, because in this case u absolutely no need a second column in your database.
Do you want value_2 equal to value_1 only the creation of a new record but allow it to be edited after that? In this case you should put the logic in your model with a callback :
In your model add :
before_create :set_value_2
private
def set_value_2
self.value_2 = value_1
end
The private statement is because you don't want anybody to access the method outside the model itself.

Umbraco Form - Remove time from Datepicker date in email

I have a form that contains a Datepicker and in the email that is sent on submission the date is formatted with a time.
How do I remove the time from the date?
Thanks
You can either change the property to just Date, without the time element, or you can use ToString() method for the DateTime within C#:
https://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx
The format without time would look something like this:
yourDate.ToString("DD/MM/YYYY");

how to add validation on date overlaps in grails

I have a domain class where i have a field where there are two dates namely start date and stop date. I have mysql database linked with my application. While entering details in webpage, i need to make sure that the date given does not overlap with older enteries in the database.
My domain class is defined below:
class Assigned {
String assignedTo = " "
Date assignedDate
Date expiryDate
String ownedBy
String additionalInformation = " "
static constraints = {
assignedTo()
assignedDate()
expiryDate()
ownedBy()
additionalInformation()
}
}
when i press create in the view, i need to validate that no other things overlap with the current dates i have given.
please help. Thanking you in advance..
If you want to check the period between the given start and end date with the other entries.
Write a method that check for each stored period
- if the start date is greater equals the stored start date
AND
- if the start date is less equals the stored end date
Do this also for the end date.
In the controller call the method from the save/update method. If start or end date overlap with another period you create a validation failed message + redirect to the create/edit view.

Turn off Mvc Date Picker Validation

I have a date field in my grid. I can't seem to figure out how can I turn off the validation if a user doesn't enter a date. In my data model the date property is not decorated with required attribute. Also in the database that I am using the date column is allowed a null and has a default value. I also turned off the Client Validation in the web config file. But still can't figure out why the date field has client validation turned on. The telerik version I am using is 2011.2.712.340.
So I was able to find out how to turn off the client validation if the user is not required to enter a date while filling a form. When declaring the data model if you have the property to as nullable:
public DateTime? YourDateProperty {get; set;}
then you can edit or insert a new record. In my case I had the date field as one of the columns in the grid and I was having problems where I could not insert a new record without the user having to enter a date.

Resources