klen / mixer

Mixer -- Is a fixtures replacement. Supported Django, Flask, SqlAlchemy and custom python objects.
Other
939 stars 96 forks source link

Django 2.0 mixer.blend fails to populate model that has foreign key #105

Open denisvolokh opened 5 years ago

denisvolokh commented 5 years ago

Hi,

Here are my models:

class Instrument(models.Model):
    stock_code = models.CharField(max_length=50)
    asset_type = models.ForeignKey(AssetType, on_delete=models.DO_NOTHING)
    data = JSONField()

    def __str__(self):
        return self.stock_code
class AssetType(models.Model):
    code = models.CharField(max_length=5)
    name = models.CharField(max_length=25)

    def __str__(self):
        return self.name

When I try to test Instrument model:

class TestInstrument:
    def test_model(self):

        obj = mixer.blend(Instrument)

        assert obj.pk == 1, "Should save an Instrument instance"

I receive following error:

_____________________________________________________________________________ TestInstrument.test_model _____________________________________________________________________________

self = <django.db.models.fields.AutoField: id>, value = <AssetType: Jared Smith>

    def to_python(self, value):
        if value is None:
            return value
        try:
>           return int(value)
E           TypeError: int() argument must be a string, a bytes-like object or a number, not 'AssetType'

../venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py:940: TypeError

During handling of the above exception, another exception occurred:

self = <strategy.tests.test_models.TestInstrument object at 0x1073fedd8>

    def test_model(self):

>       obj = mixer.blend(Instrument)

strategy/tests/test_models.py:34:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../venv/lib/python3.6/site-packages/mixer/main.py:568: in blend
    return type_mixer.blend(**values)
../venv/lib/python3.6/site-packages/mixer/main.py:116: in blend
    for name, value in defaults.items()
../venv/lib/python3.6/site-packages/mixer/main.py:116: in <genexpr>
    for name, value in defaults.items()
../venv/lib/python3.6/site-packages/mixer/mix_types.py:222: in gen_value
    return type_mixer.gen_field(field)
../venv/lib/python3.6/site-packages/mixer/backend/django.py:270: in gen_field
    return super(TypeMixer, self).gen_field(field)
../venv/lib/python3.6/site-packages/mixer/main.py:193: in gen_field
    return self.gen_value(field.name, field, unique=unique)
../venv/lib/python3.6/site-packages/mixer/main.py:261: in gen_value
    return self.get_value(field_name, value)
../venv/lib/python3.6/site-packages/mixer/backend/django.py:218: in get_value
    return self._get_value(name, value, field)
../venv/lib/python3.6/site-packages/mixer/backend/django.py:233: in _get_value
    value = field.scheme.to_python(value)
../venv/lib/python3.6/site-packages/django/db/models/fields/related.py:874: in to_python
    return self.target_field.to_python(value)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <django.db.models.fields.AutoField: id>, value = <AssetType: Jared Smith>

    def to_python(self, value):
        if value is None:
            return value
        try:
            return int(value)
        except (TypeError, ValueError):
            raise exceptions.ValidationError(
                self.error_messages['invalid'],
                code='invalid',
>               params={'value': value},
            )
E           django.core.exceptions.ValidationError: ["'Jared Smith' value must be an integer."]

../venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py:945: ValidationError

What is wrong with my models?

Thanks

mahaffey commented 5 years ago

Are you 100% sure you are using Django 2.0 and not 2.1?

Mixer is currently incompatible with 2.1.

mahaffey commented 5 years ago

If you must use 2.1 you can see this PR.

https://github.com/klen/mixer/pull/104

denisvolokh commented 5 years ago

Awesome! Thank you.

Kind regards, Denis Volokh

On Tue, Sep 11, 2018 at 10:34 PM Ryan Mahaffey notifications@github.com wrote:

If you must use 2.1 you can see this link.

104 https://github.com/klen/mixer/pull/104

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/klen/mixer/issues/105#issuecomment-420296094, or mute the thread https://github.com/notifications/unsubscribe-auth/AAP9IlOTmEet5vLjsq0DtI-ZTI9gHSv_ks5uZ8n0gaJpZM4Wi7hf .

bobbyrenwick commented 5 years ago

This is resolved in v6.1.3!