GithubHelp home page GithubHelp logo

Comments (5)

berinhard avatar berinhard commented on August 28, 2024 2

@therms I don't know if I understood your issue, but the following approach worked for me in previous experiments:

emp1 = Employer.object.create(**kwargs)
emp2 = Employer.object.create(**kwargs)
emp3 = Employer.object.create(**kwargs)
person = mommy.make(Person, employers=[emp1, emp2, emp3])

Another way to make model_mommy to create a Employer object is adding a generator function to your custom field. This could be done as the following:

from model_mommy import Mommy
from your_app.models import YourCustomField, Person

def your_custom_field_generator():
    '''
    A function to handle the custom field random value generation
    '''
    return "42"

person_mommy = Mommy(Person)
self.type_mapping.update({YourCustomField: you_custom_field_generator})

Maybe we can improve this usage in the docs...

from model_mommy.

dmwyatt avatar dmwyatt commented on August 28, 2024

I like the first thing you mention, but if I try it I get the following traceback (running model_mommy from git):

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/dustin/.virtualenvs/lalo-django/local/lib/python2.7/site-packages/model_mommy/mommy.py", line 52, in make
    return mommy.make(**attrs)
  File "/home/dustin/.virtualenvs/lalo-django/local/lib/python2.7/site-packages/model_mommy/mommy.py", line 210, in make
    return self._make(commit=True, **attrs)
  File "/home/dustin/.virtualenvs/lalo-django/local/lib/python2.7/site-packages/model_mommy/mommy.py", line 241, in _make
    self.m2m_dict[field.name] = self.m2m_value(field, model_attrs)
  File "/home/dustin/.virtualenvs/lalo-django/local/lib/python2.7/site-packages/model_mommy/mommy.py", line 257, in m2m_value
    value = model_attrs.pop(field.name)
NameError: global name 'model_attrs' is not defined

from model_mommy.

berinhard avatar berinhard commented on August 28, 2024

@therms thanks for reporting this bug! I've made the PR #83 to fix it and @vandersonmota had already merged it on the master branch. So, if you update your model_mommy version using the current master branch, the code that I wrote on the last comment will work.

If everything works fine, please, don't forget to close the issue.

from model_mommy.

vandersonmota avatar vandersonmota commented on August 28, 2024

can i close this issue?

from model_mommy.

dmwyatt avatar dmwyatt commented on August 28, 2024

I haven't had the time to test it out, so its up to whether or not you think @berinhard really fixed it. If I test it soon, I'll close the issue if you haven't by then.

from model_mommy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.