model-bakers / model_bakery

Object factory for Django
https://model-bakery.readthedocs.io/en/latest/
Other
846 stars 85 forks source link

random date in multiple date fields in order #482

Closed questionanswerguy closed 2 months ago

questionanswerguy commented 5 months ago

Greetings

im trying to generate random date in multiple date fields in order

take this model for example: -

def ticket (models.model):
    name = models.chartField(max_length=200, unique=True)
    created_date = models.DateTimeField()
    update_1_date = models.DateTimeField(null=True, blank=True)
    update_2_date = models.DateTimeField(null=True, blank=True)
    update_3_date = models.DateTimeField(null=True, blank=True)

the date fields values should be created in order as in update_1_date value should be after created_date update_2_date value should be after update_1_date update_3_date value should be after update_2_date

for example: - created_date = 2024-02-22 update_1_date = 2024-04-09 update_2_date = 2024-05-14 update_3_date = 2024-10-24

any idea on how to do this using model-bakery?

amureki commented 4 months ago

Greetings!

Just to make sure we are on the same page I would like to know some more details:

  1. Are you trying to generate random dates in your tests or directly in the model code?
  2. Did you consider setting up Django DB constraints?
  3. Can you provide a valid snippet of your code? I doubt the one you posted is going to work - please, take a look at Django models documentation to see how to define a model class.
berinhard commented 2 months ago

@questionanswerguy unfortunatelly this scenario is not covered by model-bakery since Django has its internal behavior to populate datetime fields when one of their auto_now or auto_now_add flags is enabled. In https://github.com/model-bakers/model_bakery/issues/23 we've had a discussion about this topic and the developers agreed on not adding this to model-bakery in favor to rely on other testing tools to control automatic datetime values. From my experience, I really recommend freezegun.

Due to the issue inactivity and the topic of discussion, I'm closing this one.