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...@googlegroups.com
> <mailto:
django-users...@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.