Skip to content

Conversation

@amureki
Copy link
Member

@amureki amureki commented Jan 30, 2026

Describe the change
Adds a defensive check to skip ForeignObjectRel instances (reverse relations like ManyToOneRel) in _skip_field().
These objects don't have has_default() and other Field attributes, which can cause AttributeError in certain model configurations.

I could not yet reproduce the issue with test models, this is why tests are a bit convoluted. Will follow up on this one.

PR Checklist

  • Change is covered with tests
  • CHANGELOG.md is updated if needed

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an AttributeError that occurred when processing models with reverse ForeignKey relations when optional field population was enabled.

✏️ Tip: You can customize this high-level summary in your review settings.

@amureki amureki self-assigned this Jan 30, 2026
@coderabbitai
Copy link

coderabbitai bot commented Jan 30, 2026

📝 Walkthrough

Walkthrough

This PR fixes an AttributeError when processing Django models with reverse ForeignKey relations (ManyToOneRel) by implementing logic to properly identify and skip these fields during model baking, along with corresponding regression test coverage.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md
Added entry documenting the fix for AttributeError when processing models with reverse ForeignKey relations (issue #575).
Core Logic
model_bakery/baker.py
Imported ForeignObjectRel and modified Baker._skip_field to identify and skip reverse relation fields, ensuring they are not treated as regular fields during baking.
Test Coverage
tests/test_baker.py
Added TestReverseRelations class with a regression test that verifies _skip_field correctly returns True for reverse relations when fill_optional is enabled.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit hops through relations so fine,
Skipping reverse links in code so divine,
No AttributeError shall cross our path,
With ManyToOneRel we've fixed the wrath! 🌟

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix AttributeError on ManyToOneRel fields (#575)' directly and clearly describes the main fix implemented in this PR: addressing an AttributeError issue specifically with ManyToOneRel (reverse relation) fields.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch 575/attribute-error-many-to-one-rel

Tip

🧪 Unit Test Generation v2 is now available!

We have significantly improved our unit test generation capabilities.

To enable: Add this to your .coderabbit.yaml configuration:

reviews:
  finishing_touches:
    unit_tests:
      enabled: true

Try it out by using the @coderabbitai generate unit tests command on your code files or under ✨ Finishing Touches on the walkthrough!

Have feedback? Share your thoughts on our Discord thread!


Comment @coderabbitai help to get the list of available commands and usage tips.

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

Successfully merging this pull request may close these issues.

2 participants