Thanks Mike, i think the lack of understanding of the underlying 
implementation is what was tripping me up.  Data tracked by this "field" 
actually being a different table at the DB level makes complete sense. I 
have added the M2M field to my ticket model and now have this working at 
the DB level.  

For anyone who stumbles on this in the future, lets take my sudo-models 
below, if i add a field subscribers to ticket, as a M2M field to User, I 
can now add 1 or more User objects to said "field" which is actually a 
table, in this example appname_ticket_subscribers which i can add/remove 
users to with ticket.subscribers.[add/remove].(userObject).  

example models
User:
username = charfield
other things....

Ticket:
requester = fk to user
owner = fk to user
*subscribers = models.ManyToManyField(User) <-- the new field*
other things....

and in the DB 
db=# select * from app_ticket_subscribers;
 id | ticket_id | user_id 
----+---------------+---------
  1 | 000001      |       1
(1 row)


The relevant KB for anyone trying to implement a similar thing:
https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/
On Thursday, December 8, 2022 at 5:02:05 PM UTC-7 Mike Dewhirst wrote:

> On 9/12/2022 3:04 am, Joshua Corlin wrote:
>
> Ive not used this field before so if it is im having a hard time wrapping 
> my head around how this would work in this use case.  
>
>
> Many-to-many is simple to conceptualise if you realise it is not a field 
> in the table you think it is.
>
> It is actually a separate table containing two foreign key fields. One 
> points to a particular record in your target table and the other points to 
> a particular target record in either the same table or a different table.
>
> Django makes this easy by defining a many-to-many field in the model 
> definition but it actually uses that definition to create the separate 
> relationship table (the "through" table) with two foreign keys.
>
> The nice thing is the "through" model can also be included explicitly 
> among other model definitions. Doing so lets you add other fields to it 
> which can describe the relationship. 
>
> It is definitely worthwhile studying the docs to get this right because it 
> lets you model the real world much more accurately. 
>
> There are migration gotchas when you explicitly define models which have 
> already been created implicitly with Django's many-to-many field but there 
> are documented solutions.
>
> Cheers
>
> Mike
>
> -- 
> 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/ca893628-f140-4810-8f08-ce12daaa9a9dn%40googlegroups.com.

Reply via email to