berinhard / model_mommy

No longer maintained, please migrate to model_bakery
http://model-bakery.readthedocs.org/
Other
903 stars 141 forks source link

Keyerror for field in a model #190

Closed bwarren2 closed 10 years ago

bwarren2 commented 10 years ago

I have a model like so:

class Player(models.Model):
    steam_id = models.BigIntegerField(
        help_text="Valve's internal map",
        unique=True,
        validators=[validate_32bit],
        db_index=True
    )

    def save(self, *args, **kwargs):
        # That magic number is the valve 32bit -64bit adder.
        # Steam ids are 32 bit by convention.
        self.steam_id = self.steam_id % ADDER_32_BIT
        super(Player, self).save(*args, **kwargs)

    def is_masked(self):
        #That's the magic number for anonymous data
        return self.steam_id == ANONYMOUS_ID

    def get_64_bit_id(self):
        return self.steam_id + ADDER_32_BIT

    def get_32_bit_id(self):
        return self.steam_id % ADDER_32_BIT

    def __unicode__(self):
        return unicode(self.steam_id)

and I want to make a recipe like so:

from model_mommy.recipe import Recipe, seq

from players.models import Player

player = Recipe(
    Player,
    steam_id=seq(1),
)

but the field is apparently not recognized by model_mommy:

Traceback (most recent call last):
  File "/home/ben/Projects/datadrivendota/datadrivendota/datadrivendota/heroes/tests/tests.py", line 158, in setUp
    p = mommy.make_recipe('players.player',)
  File "/home/ben/.virtualenvs/ddd_smoke/local/lib/python2.7/site-packages/model_mommy/mommy.py", line 86, in make_recipe
    return _recipe(mommy_recipe_name).make(_quantity=_quantity, **new_attrs)
  File "/home/ben/.virtualenvs/ddd_smoke/local/lib/python2.7/site-packages/model_mommy/recipe.py", line 43, in make
    return mommy.make(self.model, **self._mapping(attrs))
  File "/home/ben/.virtualenvs/ddd_smoke/local/lib/python2.7/site-packages/model_mommy/recipe.py", line 28, in _mapping
    mapping[k] = self._iterator_backups[k][1]
KeyError: 'steam_id'

Does this have to do with the custom validator? What should be my workaround here?

vandersonmota commented 10 years ago

I wasn't able to reproduce this bug. Which version did you used?

bwarren2 commented 10 years ago

I am on model-mommy==1.2.1.

bwarren2 commented 10 years ago

More errors :( . I am apparently having some deep issue with seq s. When I have no seq fields, the generation works correctly; when I add them, I get keyerrors. https://gist.github.com/bwarren2/f0b296fff71b78c42488

vandersonmota commented 10 years ago

@bwarren2 i'll dig deeper this wekkend. thanks for the report.

bwarren2 commented 10 years ago

Upping to 1.2.2 seems to have fixed it!

vandersonmota commented 10 years ago

:)