model-bakers/model_bakery

AttributeError: 'ManyToManyRel' object has no attribute 'has_default'

a-edakin opened this issue · 9 comments

After update from 1.3.2 to 1.3.3 started getting exception from title.

Sorry I didn't debug properly this issue and can't say why this is happening but my best guess would be because of this change 1.3.2...1.3.3#diff-e5857deb915e241f429a0c118e89e06a3388d3ce1466e3aa4b960b7055172b6dL322

Expected behavior

Baker.get_fields() 1.3.2 version
(
    <django.db.models.fields.AutoField: id>,
    <django.db.models.fields.related.ForeignKey: group>,
    <django.db.models.fields.related.ManyToManyField: service_lines>,
)

Actual behavior

Baker.get_fields() 1.3.3 version
{
    <django.db.models.fields.AutoField: id>,
    <django.db.models.fields.related.ForeignKey: group>,
    <django.db.models.fields.related.ManyToManyField: service_lines>,
    <ManyToManyRel: myapp.foo1>,  # I guess it not suppose to be here
}

And as a result of new element from Baker.get_fields()

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/python3.8/site-packages/model_bakery/baker.py:89: in make
    return [
/python3.8/site-packages/model_bakery/baker.py:90: in <listcomp>
    baker.make(
/model_bakery/baker.py:324: in make
    return self._make(**params)
/model_bakery/baker.py:371: in _make
    self.model_attrs[field.name] = self.generate_value(

>       if field.has_default() and field.name not in self.rel_fields:
E       AttributeError: 'ManyToManyRel' object has no attribute 'has_default'

/model_bakery/baker.py:566: AttributeError

Reproduction Steps

I don't think that model that I use has any custom behavior and it's just because of how Baker.get_fields() works in new 1.3.3 version

Models that I use anyway

class Foo(models.Model):
    slug = models.SlugField("Service line slug", unique=True, max_length=150)
    name = models.CharField("Service line name", max_length=150, null=True)

class Foo1(models.Model):
    bars = models.ManyToManyField("myapp.Bar")

class Bar(models.Model):
    foo = models.ManyToManyField("myapp.Foo", related_name="foos")

baker.make("core.Bar", _quantity=3, slug=cycle(["1", "2", "3"]), _fill_optional=True)

Versions

Python: 3.8.10
Django: 2.2.24
Model Bakery: 1.3.3

My guess (without diving into the code) is that it was a result of this change: 5d1be50 from #237.

Thanks for reporting this issue @a-edakin! We'll deploy the fix within the next release.

After @ashiazed PRs, I think this is the only one which prevents us from a new release.

pheki commented

@berinhard thanks a lot for maintaing this!

Is this still planned for next release? Looks like 1.4.0 has been released in the meantime

pheki commented

Wow, that was fast! Obrigado de novo! (thanks again 😉 )

Your comment helped me to remind myself about how long I was away of the project. Thank you =)

Anyone know when a release will be made with this fix?

HI @epicserve! It has been deployed in 1.5.0 release

Thanks a lot for this c: