mardi 4 août 2015

overriding methods from original model in fake model Factory Boy (Python)

I have a unicode method in a real model, model_1:

def __unicode__(self):
    return u"E%s-%08d-%s" % (str(self.start_date.year)[2:], self.id, self.model_2_id)

So far, I've tried overriding the unicode for fake model 1 several different ways, giving fake model 1 and 2 an integer id, but the problem is I can only use build() and not create() based on the system.

class Model_1(DjangoModelFactory):
    class Meta:
        model = 'app.Model_1'
    field  = factory.SubFactory('testing.factories.SomeModel')
    id     = 2

I was hoping m = Model_1.build() would be able to access m.id, but using create() and saving the instance is not possible for me at all. I changed the title to changing methods in general because the issue was from this other model not allowing saves:

def save(self, *args, **kwargs): raise Exception("This model doesn't allow save!")

in system it loads from yaml. Factory boy does not show how to make a single class from yaml, in fact the point of FBoy is to NOT have to load from yaml anymore.

Adding @classmethod before a method in fake factories makes it recognize the name as valid, but it doesn't overtake the original method when making a fake factory. I ask

  1. how can I override method in the fake classes
  2. why is unicode being called in the original model when these fake ones are made? I thought unicode is only for displaying info when using an existing object?

thank you

Aucun commentaire:

Enregistrer un commentaire