Skip to content

Conversation

@amureki
Copy link
Member

@amureki amureki commented Jan 21, 2026

Describe the change
I figured that bulk_create was using incorrect properties (the related model's name/related_name) for getting FK names.

Replaced field.remote_field.name and field.related_model._meta.model_name with Django's field.m2m_field_name() and field.m2m_reverse_field_name() (ref) which return the correct FK field names for both auto-created and custom through models.

PR Checklist

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed bulk creation of objects with many-to-many relationships when using custom through models.
  • Tests

    • Added regression tests for bulk creation with many-to-many relationships and through models.

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

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

coderabbitai bot commented Jan 21, 2026

📝 Walkthrough

Walkthrough

Fixed bulk_create to properly handle ManyToManyField relationships with custom through models by switching from using remote field names and model meta names to using M2M helper methods field.m2m_field_name() and field.m2m_reverse_field_name() for correct key instantiation.

Changes

Cohort / File(s) Summary
Bug fix
model_bakery/baker.py
Modified through-model field value construction in bulk_create to use M2M helper methods instead of manually constructed field names for M2M relationships
Test coverage
tests/test_baker.py
Added two regression tests: test_bulk_create_with_m2m and test_bulk_create_with_m2m_through_model validating bulk creation of M2M relationships with and without custom through models
Documentation
CHANGELOG.md
Added changelog entry documenting the M2M bulk_create fix for custom through models

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Through fields we hop with names so right,
M2M relations, shining bright!
From manual paths to helper ways,
Bulk create works in modern days! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.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 accurately summarizes the main change: fixing _bulk_create for ManyToManyField with custom through models, which is directly reflected in the code changes to model_bakery/baker.py.

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

✨ Finishing touches
  • 📝 Generate docstrings

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

@amureki amureki merged commit 8b555c9 into main Jan 21, 2026
45 checks passed
@amureki amureki deleted the 477/m2m-through-via-bulk branch January 21, 2026 19:51
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