New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory attribute with reserved name (attributes). #206

Closed
kevinastone opened this Issue May 7, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@kevinastone

Have a model with a field name attributes that I'm unable to create due to it being a reserved method on the factory:

class TestFactory(factory_boy.Factory):
    attributes = 'something'

Results in:

>>> TestFactory.create()
*** TypeError: 'str' object is not callable

Is there a way to alias the attribute names to avoid this collision?

rbarrois added a commit that referenced this issue May 20, 2015

Add Meta.rename to handle name conflicts (See #206).
Define ``Meta.rename = {'attrs': 'attributes'}``
if your model expects a ``attributes`` kwarg but you can't
define it since it's already reserved by the ``Factory`` class.
@rbarrois

This comment has been minimized.

Show comment
Hide comment
@rbarrois

rbarrois May 20, 2015

Member

Hi,

This should be fixed in the next release (hopefully within a couple of weeks), with Meta.rename.

In the meantime, you can also use the _adjust_kwargs method:

class TestFactory(factory.Factory):
    attrs = 'something'

    @classmethod
    def _adjust_kwargs(cls, kwargs):
        kwargs['attributes'] = kwargs.pop('attrs')
        return kwargs
Member

rbarrois commented May 20, 2015

Hi,

This should be fixed in the next release (hopefully within a couple of weeks), with Meta.rename.

In the meantime, you can also use the _adjust_kwargs method:

class TestFactory(factory.Factory):
    attrs = 'something'

    @classmethod
    def _adjust_kwargs(cls, kwargs):
        kwargs['attributes'] = kwargs.pop('attrs')
        return kwargs

@rbarrois rbarrois closed this May 20, 2015

@rbarrois rbarrois added the Bug label May 20, 2015

@ibushong

This comment has been minimized.

Show comment
Hide comment
@ibushong

ibushong May 22, 2015

Minor typo in your last post, kwargs --> **kwargs. Thanks for this though, solved my issue too.

Minor typo in your last post, kwargs --> **kwargs. Thanks for this though, solved my issue too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment