lundi 27 août 2018

Factory-boy property attributes?

I have a recipe like this:

import factory
from models import Foobar


class MenuItemFactory(factory.Factory):
    class Meta:
        model = MenuItem

    name = 'Default Foobar'
    slug = factory.LazyAttribute(lambda o: '%s' % o.name)

I want to add dynamic properties, like slug, but I want to do it in a separate method. I want this, since writing any more complex logic in lambda one-liners would be, well, ugly.

One thought came to mind, using a property method, like they do in Django models. For example:

class MenuItemFactory(factory.Factory):
    ...

    @property
    def slug(self):
        return '%s' % self.name

Is there a way, similar to this, that will accomplish what I'm trying to do?

Aucun commentaire:

Enregistrer un commentaire