Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
denisvolokh opened this issue Sep 11, 2018 · 4 comments
Open

Comments

@denisvolokh
Copy link

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
Copy link

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

Mixer is currently incompatible with 2.1.

@mahaffey
Copy link

mahaffey commented Sep 11, 2018

If you must use 2.1 you can see this PR.

#104

@denisvolokh
Copy link
Author

denisvolokh commented Sep 11, 2018 via email

@bobbyrenwick
Copy link

This is resolved in v6.1.3!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants