1đź‘Ť
From what I understood, it seems that you misunderstand how M2M work.
In your case, it seems that a message can ben sent to many numbers. And a number can receive many messages -> You DO need a Many2Many relation. There is no other solution.
In a “conceptual way” (in Django), when you design a M2M relation, you define 2 models (UserCellPhone
and SentMessage
) but in reality (ie. what’s happening at the database level), there will be 3 tables involved because Django will create a table to link your both models with a M2M relation.
If you want to specify the third table used with Django, you can do it this way, using your example:
class UserCellPhone(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
class SentMessage(models.Model):
to_number = models. ManyToManyField(UserCellPhone, through='MessagesDelivery')
This way, Django will create 3 tables, and the MessagesDelivery table will contain couples with a FK to UserCellPhone + a FK to SentMessage.
For example, if message_38 is sent to usercellphone_45 and usercellphone_67, your MessagesDelivery table will look like:
-- sent_message_id - usercellphone_id
-- 38 - 45
-- 38 - 67
This is how a M2M relation work (not only in Django).