简体   繁体   English

如何使用model_mommy模拟从TimeStampedModel继承的模型的创建字段?

[英]How can I mock the created field of a model that inherits from TimeStampedModel using model_mommy?

I am trying to test a date filter but have been unable to set the created date using mommy.make(). 我正在尝试测试日期过滤器,但无法使用mommy.make()设置创建的日期。 When I make the objects with model mommy the created field is set to the time the objects were created rather than what I passed in with mommy.make() 当我使用模型mommy制作对象时,创建的字段设置为创建对象的时间,而不是我使用mommy.make()传递的时间

def test_mommy(self):
    today = arrow.now()
    yesterday = today.replace(days=-1)

    mommy.make('Model', created=today.naive)
    mommy.make('Model', created=yesterday.naive)

    model_1_created = Model.objects.all()[0].created
    model_2_created = Model.objects.all()[1].created

    self.assertNotEqual(model_1_created.strftime('%Y-%m-%d'), model_2_created.strftime('%Y-%m-%d'))

This test fails with the Assertion Error: 该测试失败并出现断言错误:

AssertionError: '2018-03-15' == '2018-03-15'

I may have a misunderstanding of how model_mommy creates these objects. 我可能对model_mommy如何创建这些对象有误解。 But I would think this should create it and set the created dates properly. 但是我认为这应该创建它并正确设置创建日期。 Though it looks like the default TimeStampedObject behavior is taking over. 虽然看起来默认的TimeStampedObject行为正在接管。

I was able to save the created by dates after the objects had been created. 创建对象后,我能够保存创建日期。 I think this could have also been achieved by overriding the save method on TimeStampedModel. 我认为也可以通过重写TimeStampedModel上的save方法来实现。 But this seemed like the simpler solution. 但这似乎是更简单的解决方案。

def test_mommy(self):
    today = arrow.now()
    yesterday = today.replace(days=-1)

    foo = mommy.make('Model')
    bar = mommy.make('Model')

    foo.created = today.naive
    foo.save()
    bar.created = yesterday.naive
    bar.save()

    model_1_created = Model.objects.all()[0].created
    model_2_created = Model.objects.all()[1].created

    self.assertNotEqual(model_1_created.strftime('%Y-%m-%d'), model_2_created.strftime('%Y-%m-%d'))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 model_mommy打破了django-mptt - model_mommy breaks django-mptt 使用Django 1.4,如何在不自动更新修改后的字段的情况下保存TimeStampedModel对象? - Using Django 1.4, How can I save TimeStampedModel objects without automatically updating the modified field? 在odoo中删除子模型(使用继承的主模型创建)的记录时,如何删除主模型的记录 - How to delete records of main model when records of child model (created using inherits main model) deleted in odoo 如何继承继承 db.Model 的 class - How can I inherit a class which inherits db.Model 如何模拟 django model object? - How can I mock django model object? Django:使用Model-Mommy测试自定义ModelForm的“清理”方法 - Django: Testing the 'clean' method of a custom ModelForm using Model-Mommy 如何以我想要的格式获取创建字段的日期(django model 字段)? - How can I get the date of the created field(django model field) in the format I want? 如何使用一个模型中的字段信息来计算其他模型中的另一个字段? - How can I use the information of a field from one model to calculate another field in other model? 如何制作 Django model 表单,其字段名称与 model 字段名称不同? - how can I make a Django model form with a field name in the form different from the model field name? 如何显示从 model 到 Django 模板的元组字段? - How can I display a tuple field from a model to a template in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM