There are two lists of the same length that are relevant to the two fields in the model. How to add these two lists synchronously in the database? I suspect that this can somehow be done in 1 request, because not to do it is stupid in a loop?
1 answer
model_object.field1.add(*list1) model_object.field2.add(*list2)
of course field1
and field2
should be ManyToManyField
Generally need to read the documentation .
- Only with ManyToMany? I do a massive import through the admin panel of the usual charset fields, added my view with textarea, parsed regular sheets into two sheets, I can understand .save () in the loop, but I think this is somehow clumsy, so I ask if there is another method. Googled such a thing docs.djangoproject.com/en/dev/ref/models/querysets/… but this is not for lists - Volodya Kogan
- Those. lists do not contain model objects, but some data and this data individually need to be assigned to the model fields from the first list to the first field, and from the second to the second? If this is the case, in the case of already existing records to which you need to add list data, you cannot do without a cycle (or make [RAW SQL query] [1] updating fields), but if there are no records yet and they need to be created, then you can use [bulk_create] [2] [1]: django.readthedocs.org/en/1.4.X/topics/db/sql.html [2]: django.readthedocs.org/en/1.4.X/ref/models/… - qnub
|