On 23/06/2021 7:18 pm, Jayanth Shankar wrote:

Hi,

I am trying to add items to a ManyToMany relationship, but I would like to make the operation use a particular database connection, like one might do when calling save() on a model instance. However, looking at the source for ManyToMany.add(), the function defaults to checking the router's for_write connection.

class B(models.Model):
...

class A(models.Model):
b = models.ManyToManyField(B)
...

ideally, I would like to modify the many to many field as follows...
a = A(id=0)
for b in B.objects.all():
    a.b.add(b, using="aconnection")

but this does not work. As far Is there an alternative way to insert an object into the ManyToMany relationship(where a db alias can be specified) besides add?

Not sure I understand what you are trying to do but it is possible to manage the many-to-many yourself. Django makes a table to connect A and B without your needing to specify it as a model.

If you write a model to represent that table you can add other fields to it and use its save() method to perform all sorts of magic.

For example, I have chemicals and products.

class Chemical(models.Model):

    products = models.ManyToManyField(

        "Product",

        blank=True,

        through="ChemicalProducts",

        through_fields=("chemical", "product"),

        related_name="products",

    )


class ChemicalProducts(models.Model):

    chemical = models.ForeignKey(

        "Chemical",

        on_delete=models.CASCADE,

        related_name="base_chemical",

        null=True,

        blank=True,

    )

    product = models.ForeignKey(

        "Product",

         on_delete=models.CASCADE,

        null=True,

        blank=True,

    )

    # other fields and methods follow here

If I did not write the ChemicalProducts model, Django would make it anyway. But because I have named the through table and it exists Django doesn't bother.

This means I can write queries which insert m2m connections without using 'add'.

However, I don't know because I haven't looked at the docs or the Django source, whether you can use a callable for the 'through' table or even specify the db_alias for it. I have only ever used the default myself.

HTH

Mike





--
You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a49409db-5b9a-48cd-818f-6645caa184e9n%40googlegroups.com <https://groups.google.com/d/msgid/django-users/a49409db-5b9a-48cd-818f-6645caa184e9n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.


--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0e161c3e-9c55-450b-e289-644e9fcfcfd4%40dewhirst.com.au.

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to