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

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

Closed
a-edakin opened this issue Oct 14, 2021 · 9 comments · Fixed by #299
Closed

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

a-edakin opened this issue Oct 14, 2021 · 9 comments · Fixed by #299
Labels
bug Something isn't working high priority

Comments

@a-edakin
Copy link

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

@timjklein36
Copy link
Collaborator

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

@berinhard
Copy link
Member

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

@berinhard berinhard added the bug Something isn't working label Oct 29, 2021
@berinhard
Copy link
Member

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

codeguru42 added a commit to BaseballCardTracker/bbct-api that referenced this issue Feb 15, 2022
Cannot update to past 1.3.2 at this time because of a bug
with `ManyToMany` fields. See model-bakers/model_bakery#248.
@pheki
Copy link

pheki commented Mar 31, 2022

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

pheki commented Mar 31, 2022

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

@berinhard
Copy link
Member

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

@epicserve
Copy link

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

@berinhard
Copy link
Member

HI @epicserve! It has been deployed in 1.5.0 release

@el-chogo
Copy link

el-chogo commented Apr 7, 2022

Thanks a lot for this c:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants