------
The most dangerous phrase in the language is, "We've always done it this
way."

- Grace Hopper

On 11 April 2017 at 05:35, Cassium <mark.c.my...@gmail.com> wrote:

> Say I am collecting residence history and I allow the user to submit up to
> five previous addresses. I have a PEOPLE model and RESIDENCES model and
> anticipate a many-to-many relationship between. Back when I was working
> with Joomla I would have manages these models (well, tables) with a table
> in the middle called PEOPLE_RESIDENCES which would have also included the
> dates of that relationship. And I understand that I can do that with a
> model and the 'through' statement.
>
> But here is where I'm lost. When I get a form submitted from the user, how
> do I get all the correct rows written to the various tables. Assume for
> simplicity sake that I have a new person being submitted as follows
>
> PERSON
> --RESIDENCE 1, 2005-2008
> --RESIDENCE 2, 2008-2011
> --RESIDENCE 3, 2012-2016
>
> So I would need one new row on the PEOPLE table, three new rows on the
> PEOPLE_RESIDENCES table and up to three new rows on the RESIDENCES table.
> I'm lost on how the necessary rows are added to the PEOPLE_RESIDENCES
> table. Is there some sort of value returned from the create/insert
> statements or do I send the ORM a structured object which manages all of
> the INSERTS including the relatioinships? Or is there something else I have
> to do?
>
>
Have you read this part of the documentation? It describes exactly what you
need:

https://docs.djangoproject.com/en/1.11/topics/db/models/#extra-fields-on-many-to-many-relationships

With regard to "how do I get the correct record written to the right table"
you are putting the cart before the horse.

The whole idea of an ORM is to disappear that type of consideration
(despite it being exactly what *is* happening in the background).

In the example of the documentation, when you create a Group, it is "saved
to the correct table when you call the save() method (successfully)". When
you create a Person, it is also saved to the correct table when you call
the save() method.

When you create a Membership that links to both the Person and the Group,
it too is saved to the correct table when you call the save() method.

Cheers
L.

-- 
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 post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAGBeqiPXxcm-OG8mD1PCo3%3Dx%2BmkUw1y5DthpQ-EOqAGios%3DQ8g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to