# Add multiple entries to many-to-many field with different through\_defaults for each entry

**URL:** <https://forum.djangoproject.com/t/add-multiple-entries-to-many-to-many-field-with-different-through-defaults-for-each-entry/8540>\
**Category:** Using Django\
**Created:** [June 26, 2021, 7:02pm UTC](https://forum.djangoproject.com/t/add-multiple-entries-to-many-to-many-field-with-different-through-defaults-for-each-entry/8540 "2021-06-26T19:02:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samul-1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/samul-1/32/4767_2.png) [@samul-1](https://forum.djangoproject.com/u/samul-1)\
**Post date:** [June 26, 2021, 7:02pm UTC](https://forum.djangoproject.com/t/add-multiple-entries-to-many-to-many-field-with-different-through-defaults-for-each-entry/8540/1 "2021-06-26T19:02:54Z")

</div>

Say I have a model `A` with a field called `m2m` which is a many-to-many field to another model `B`.

Then I want to add all the elements inside a queryset to this field at once. If `qs` is my queryset, I could do `a.m2m.add(*qs)`. However, say I’m using a through model for the relationship and that through model has a custom field `ordering`.

Is there any way I can add the whole queryset to my `m2m` field in a single instruction, while specifying a different value for the field `ordering` for each entry I’m adding? I know what for a single entry you can use `through_defaults`, but I haven’t been able to find anything for bulk creation.

I could surely do:

```auto
count = 0
for e in qs:
    a.m2m.add(e, through_defaults={'ordering': count})
    count += 1

```

But it’s not very elegant.

Is there a way to achieve it like I mentioned above? Thank you!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2021, 1:56am UTC](https://forum.djangoproject.com/t/add-multiple-entries-to-many-to-many-field-with-different-through-defaults-for-each-entry/8540/2 "2021-06-27T01:56:43Z")

</div>

Your “through” table is a table - it has at least three columns in this case, the foreign keys to the two tables and the field `ordering`.

You can work with this table the same way you work with any other table.

This means you can create individual rows the same way you create rows in any other table.

For example:

```auto
through_row = ThroughTable(a=some_a, b=some_b, ordering=count)
through_row.save()

```

And you can get as fancy with that as you wish.

---

<div class="post-metadata">

**Author:** ![samul-1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/samul-1/32/4767_2.png) [@samul-1](https://forum.djangoproject.com/u/samul-1)\
**Post date:** [June 27, 2021, 10:59am UTC](https://forum.djangoproject.com/t/add-multiple-entries-to-many-to-many-field-with-different-through-defaults-for-each-entry/8540/3 "2021-06-27T10:59:26Z")

</div>

Thank you. I guess what I was after was a more “elegant” way of doing this. For example, had I not had the requirement of having extra fields in the through table, I could have just done it in a one liner with `some_a.m2mfield.add(*qs)`, but apparently there is no such way of doing anything like that in this case.
