{"action":"closed","issue":{"url":"https://api.github.com/repos/vandersonmota/model_mommy/issues/218","labels_url":"https://api.github.com/repos/vandersonmota/model_mommy/issues/218/labels{/name}","comments_url":"https://api.github.com/repos/vandersonmota/model_mommy/issues/218/comments","events_url":"https://api.github.com/repos/vandersonmota/model_mommy/issues/218/events","html_url":"https://github.com/vandersonmota/model_mommy/issues/218","id":67117200,"number":218,"title":"Using _quantity with random name generator gives always the same value","user":{"login":"diegopau","id":315149,"avatar_url":"https://avatars.githubusercontent.com/u/315149?v=3","gravatar_id":"","url":"https://api.github.com/users/diegopau","html_url":"https://github.com/diegopau","followers_url":"https://api.github.com/users/diegopau/followers","following_url":"https://api.github.com/users/diegopau/following{/other_user}","gists_url":"https://api.github.com/users/diegopau/gists{/gist_id}","starred_url":"https://api.github.com/users/diegopau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/diegopau/subscriptions","organizations_url":"https://api.github.com/users/diegopau/orgs","repos_url":"https://api.github.com/users/diegopau/repos","events_url":"https://api.github.com/users/diegopau/events{/privacy}","received_events_url":"https://api.github.com/users/diegopau/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-04-08T12:23:37Z","updated_at":"2015-04-09T08:36:25Z","closed_at":"2015-04-09T08:36:25Z","body":"I am trying to make a simple test which should create 20 instances of a model called TagModel using Model Mommy recipes.\r\n\r\nIt seems that the generator I use to fill the tag field is only called once even if I am trying to generate 20 instances, so the test is giving me a \"duplicate key value violates unique constraint \"core_tagmodel_tag_key\".\r\n\r\nMy recipe in mommy_recipes is:\r\n\r\n```\r\ntag = Recipe(\r\n     TagModel,\r\n     tag=get_random_stuff.get_random_tag(),\r\n)\r\n```\r\nIn test_models.py I create the following simple test:\r\n\r\n```\r\nclass TagModelTestModel(TestCase):\r\n    def setUp(self):\r\n        self.tags = mommy.make_recipe('core.tag', _quantity=20)\r\n\r\n    def test_tag_creation(self):\r\n        for tag in self.tags:\r\n            print(\"tag name: \", tag.tag)\r\n\r\n        assert len(self.tags) == 25\r\n```\r\n\r\nAnd this is the generator method I use (get_random_tag()):\r\n```\r\nSECRET_KEY = 'asd538J878hdfjKEidrfdgf0954lKUJMd03l4mfjejJKkek4AA'\r\n\r\ntry:\r\n    random = random.SystemRandom()\r\n    using_sysrandom = True\r\nexcept NotImplementedError:\r\n    import warnings\r\n    warnings.warn('A secure pseudo-random number generator is not available '\r\n                  'on your system. Falling back to Mersenne Twister.')\r\n    using_sysrandom = False\r\n\r\n\r\ndef __string_generator(size=6, chars=string.ascii_uppercase + string.digits):\r\n    \"\"\"\r\n    Returns a securely generated random string.\r\n\r\n    The default length of 12 with the a-z, A-Z, 0-9 character set returns\r\n    a 71-bit value. log_2((26+26+10)^12) =~ 71 bits\r\n    \"\"\"\r\n    if not using_sysrandom:\r\n        # This is ugly, and a hack, but it makes things better than\r\n        # the alternative of predictability. This re-seeds the PRNG\r\n        # using a value that is hard for an attacker to predict, every\r\n        # time a random string is required. This may change the\r\n        # properties of the chosen random sequence slightly, but this\r\n        # is better than absolute predictability.\r\n        random.seed(\r\n            hashlib.sha256(\r\n                (\"%s%s%s\" % (\r\n                    random.getstate(),\r\n                    time.time(),\r\n                    SECRET_KEY)).encode('utf-8')\r\n            ).digest())\r\n    return ''.join(random.choice(chars) for _ in range(size))\r\n\r\n\r\ndef get_random_tag():\r\n    print(\"I've been called!\")\r\n    tag = __string_generator(size=random.randint(1, 32),\r\n                             chars=string.ascii_uppercase + string.ascii_lowercase + string.digits + '_')\r\n    print(\"New Tag!: \", tag)\r\n    return tag\r\n```\r\n"}}