Comments (5)
@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.
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.
@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.
can i close this issue?
from model_mommy.
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)
- Option of "Integer generator" generate only positive numbers HOT 1
- Request for release on PyPI HOT 2
- NullBooleanField should be treated as BooleanField(null=True, blank=True) HOT 2
- mommy does not support `CIEmailField` HOT 3
- Stick to Django support's roadmap HOT 1
- Is it possible to force it fill nullable fields? HOT 2
- Is there is a way to specify the seed for randomization? HOT 2
- Unique fields check when generating multiple objects HOT 2
- Move seq method outside recipes module HOT 1
- instance isn't saved. Use bulk=False or save the object first.
- Pass recipe to mommy.make HOT 2
- Add new issue/PR template
- Link documentation in description of repository HOT 2
- Using make_recipe with _quantity together with related key only applies to last item created
- Update documentation (readthedocs) HOT 1
- Release a new version HOT 1
- mommy.make() returns instance that did not go through any model manager, thus missing fields
- Model not saving HOT 1
- Implement lazy_related for use in override attrs in each TestCase HOT 1
- Propertis with autonow-add broke on 2.0.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from model_mommy.