Home > Blockchain >  Is there a way to add additional fields on an implicit many-to-many table in Django?
Is there a way to add additional fields on an implicit many-to-many table in Django?

Time:03-14

Consider the following models

in models.py:

class Volunteer(models.Model):
    account = models.OneToOneField(
        UserAccount,
        on_delete=models.CASCADE,
        related_name= "volunteer",
        primary_key=True)


    request_offers = models.ManyToManyField('Request', related_name="volunteers", blank = True)
    volunteer_communities = models.ManyToManyField('Community', related_name="volunteers", blank = True)
class Request(models.Model):
    req_type = models.ForeignKey('RequestTypes', related_name="requests", on_delete = models.CASCADE, blank = False, null = False)

A volunteer can have many request_offers(type of request).

In the implicit appname_volunteer_request (many-to-many) table, I want another field called date besides the default fields -- id, request_id and volunteer_id.

Is there a way to add an additional field on the implicit table without creating a through table?

Thanks! :)

CodePudding user response:

Unfortunately, there is no way to do it without a through table.

You can attempt to create one more table where the id of through table will be paired with date, and then change it whenever a signal is fired on thorough table.Other option is to manage it manually by overriding methods of the manager save, delete, create and methods of the relation manager add, clean, remove, set


Information that might help you: https://docs.djangoproject.com/en/3.2/ref/signals/#post-save https://docs.djangoproject.com/en/4.0/topics/db/models/#intermediary-manytomany

  • Related