# Buggy code
def _get_strategy_for_field(f):
    if f.choices:
        choices = []
        for value, name_or_optgroup in f.choices:
            if isinstance(name_or_optgroup, (list, tuple)):
                choices.extend(key for key, _ in name_or_optgroup)
            else:
                choices.append(value)
        if isinstance(f, (dm.CharField, dm.TextField)) and f.blank:
            choices.insert(0, "")
        strategy = st.sampled_from(choices)
    elif type(f) == dm.SlugField:
        strategy = st.text(
            alphabet=string.ascii_letters + string.digits,
            min_size=(None if f.blank else 1),
            max_size=f.max_length,
        )
    elif type(f) == dm.GenericIPAddressField:
        lookup = {
            "both": ip4_addr_strings() | ip6_addr_strings(),
            "ipv4": ip4_addr_strings(),
            "ipv6": ip6_addr_strings(),
        }
        strategy = lookup[f.protocol.lower()]
    elif type(f) in (dm.TextField, dm.CharField):
        strategy = st.text(min_size=(None if f.blank else 1), max_size=f.max_length)
    elif type(f) == dm.DecimalField:
        bound = Decimal(10**f.max_digits - 1) / (10**f.decimal_places)
        strategy = st.decimals(
            min_value=-bound, max_value=bound, places=f.decimal_places
        )
    else:
        strategy = field_mappings().get(type(f), st.nothing())
    if f.validators:
        strategy = strategy.filter(validator_to_filter(f))
    if f.null:
        strategy = st.one_of(st.none(), strategy)
    return strategy


# Traceback (ValueError)
Error
Traceback (most recent call last):
File "/vagrant/savings_champion/products/tests/models.py", line 33, in test_check_bonus
user=models(User),
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 1001, in wrapped_test
state.run()
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 725, in run
runner.run()
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/engine.py", line 393, in run
self._run()
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/engine.py", line 791, in _run
self.generate_new_examples()
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/engine.py", line 706, in generate_new_examples
self.test_function(last_data)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/engine.py", line 153, in test_function
self._test_function(data)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 691, in evaluate_test_data
escalate_hypothesis_internal_error()
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 663, in evaluate_test_data
result = self.execute(data, collect=True)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 578, in execute
result = self.test_runner(data, run)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/executors.py", line 78, in <lambda>
lambda: function(data)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/executors.py", line 33, in execute
return function()
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/executors.py", line 78, in <lambda>
lambda: function(data)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 552, in run
args, kwargs = data.draw(self.search_strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 121, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 136, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/core.py", line 180, in do_draw
return self.base.do_draw(data)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/lazy.py", line 158, in do_draw
return data.draw(self.wrapped_strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in do_draw
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 56, in newtuple
return tuple(xs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in <genexpr>
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/strategies.py", line 507, in do_draw
return self.pack(data.draw(self.mapped_strategy))
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/lazy.py", line 158, in do_draw
return data.draw(self.wrapped_strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/strategies.py", line 507, in do_draw
return self.pack(data.draw(self.mapped_strategy))
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in do_draw
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 56, in newtuple
return tuple(xs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in <genexpr>
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/lazy.py", line 158, in do_draw
return data.draw(self.wrapped_strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/strategies.py", line 1550, in do_draw
return f(draw, *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/extra/django/models.py", line 161, in _models_impl
return draw(strat)[0]
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/strategies.py", line 1548, in draw
return data.draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/strategies.py", line 507, in do_draw
return self.pack(data.draw(self.mapped_strategy))
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/lazy.py", line 158, in do_draw
return data.draw(self.wrapped_strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in do_draw
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 56, in newtuple
return tuple(xs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in <genexpr>
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/strategies.py", line 507, in do_draw
return self.pack(data.draw(self.mapped_strategy))
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in do_draw
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 56, in newtuple
return tuple(xs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/collections.py", line 60, in <genexpr>
data.draw(e) for e in self.element_strategies
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/strategies.py", line 1550, in do_draw
return f(draw, *args, **kwargs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/extra/django/models.py", line 161, in _models_impl
return draw(strat)[0]
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/strategies.py", line 1548, in draw
return data.draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 125, in draw
return self.__draw(strategy)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/internal/conjecture/data.py", line 132, in __draw
return strategy.do_draw(self)
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/searchstrategy/strategies.py", line 507, in do_draw
return self.pack(data.draw(self.mapped_strategy))
File "/home/ubuntu/.local/lib/python3.5/site-packages/hypothesis/strategies.py", line 970, in <lambda>
lambda value: target(*value[0], **value[1])
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/query.py", line 487, in get_or_create
return self.get(**lookup), False
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/query.py", line 397, in get
num = len(clone)
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/query.py", line 254, in __len__
self._fetch_all()
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/query.py", line 54, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
cursor.execute(sql, params)
File "/home/ubuntu/.local/lib/python3.5/site-packages/raven/contrib/django/client.py", line 123, in execute
return real_execute(self, sql, params)
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/ubuntu/.local/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
ValueError: A string literal cannot contain NUL (0x00) characters.

# Fixed code
def _get_strategy_for_field(f):
    if f.choices:
        choices = []
        for value, name_or_optgroup in f.choices:
            if isinstance(name_or_optgroup, (list, tuple)):
                choices.extend(key for key, _ in name_or_optgroup)
            else:
                choices.append(value)
        if isinstance(f, (dm.CharField, dm.TextField)) and f.blank:
            choices.insert(0, "")
        strategy = st.sampled_from(choices)
    elif type(f) == dm.SlugField:
        strategy = st.text(
            alphabet=string.ascii_letters + string.digits,
            min_size=(None if f.blank else 1),
            max_size=f.max_length,
        )
    elif type(f) == dm.GenericIPAddressField:
        lookup = {
            "both": ip4_addr_strings() | ip6_addr_strings(),
            "ipv4": ip4_addr_strings(),
            "ipv6": ip6_addr_strings(),
        }
        strategy = lookup[f.protocol.lower()]
    elif type(f) in (dm.TextField, dm.CharField):
        strategy = st.text(
            alphabet=st.characters(
                blacklist_characters="\x00", blacklist_categories=("Cs",)
            ),
            min_size=(None if f.blank else 1),
            max_size=f.max_length,
        )
    elif type(f) == dm.DecimalField:
        bound = Decimal(10**f.max_digits - 1) / (10**f.decimal_places)
        strategy = st.decimals(
            min_value=-bound, max_value=bound, places=f.decimal_places
        )
    else:
        strategy = field_mappings().get(type(f), st.nothing())
    if f.validators:
        strategy = strategy.filter(validator_to_filter(f))
    if f.null:
        strategy = st.one_of(st.none(), strategy)
    return strategy