Use DateField as default for another DateField

In our current entity type “Project” we want to add a DateField to the parametrization. We want to set the default of this new field to an existing DateField, such that existing instances of the Project entity will get a sensible value for this new field. I tried to do this with the LookUp() function, but that doesn’t seem to work. Is there another way to make the default value of this new field equal to an existing field? Below is the code I tried with the LookUp() function.

page_1.information.plot.moment_rent = DateField(
    "Geplande start verhuur",
    default=Lookup('page_1.information.plot.moment_sale')
)

Hi Floris,

The VIKTOR platform does not allow you to change user input “automatically”. A default is filled in at the moment that the object is created, and the value can only change by user input after this.

There are a few ways to approach this case.

  • You can initialise them both on the same value (for instance always today) but if the user changes one, the other value does not change “automatically” without the user knowing.
  • You can add some automation with a SetParams button. When the user clicks on the button, the value of the moment_rent gets updated to the current value of moment_sale. This makes the change in the value an explicit choice of the user, no values change without them knowing. This would look as follows
page1.moment_sale = DateField("Geplande Verkoop") 
page1.moment_rent = DateField("Geplande start verhuur") 
page1.update_rent =  SetParamsButton('Reset start verhuur naar verkoopdatum', method='update_moment_rent')

class MyController(ViktorController):
    ...

    def update_moment_rent(self, params, **kwargs):
        return SetParamsResult({
            "page1": {"moment_rent" : params.page1.moment_sale}
  • You can make sure the user can not continue if the moment_rent is before the moment_sale by adding input validation.

If you think this is not sufficient in your case, we would be interested in your use case, and what you think this should look like as a feature request.

kind regards,
Paulien

Hi Paulien,

Thanks for your reply. The solutions you propose are not sufficient for our use case. We don’t want to add an extra button and both date fields will not be initiated at the same moment, since the issue regards already existing projects.

To give some background, we currently let the user set a date for which they want to start selling/renting out residential properties. This date is used in all kinds of calculations. We only had one date, which we used for both sale and rent.

We now want to add the possibility for the user to use different dates for sale and rent. Projects that are already created before the new rent date field is added, used the sale date in the calculations for the output of both sale and rent. This is why we want to set the default for the rent date equal to the sale date. This way, the shown input matches what was used to create the output.

Hi Floris,

Thank you for the explanation! So, you want to add a new field, also to existing entity types. This can be achieved, and is called a “param conversion”. This means that with the new publish and the addition of the new field, the database is modified once for all the existing entities, and the new field is added an initialised the desired value.

For more information and help on how to do this, please contact your company’s support contact directly, or support@viktor.ai

Kind regards,
Paulien