Interest idea and it works, but not necessary:
    company = Company()
    #company.id is None
    companies.append(company)
    Company.objects.bulk_create(companies)
    #company.id is 1

Instead to ask pk from database, it can be used from
company_value.company.id. Django save pk value to internal meta
(models/base.py) and does not update it. This works too:
value.company = value.company     # pk updated

I want to ask, each child object should be created only when parent is
saved to db? Maybe django has something to cover this case?


On Sat, Nov 30, 2019 at 7:52 AM Charles Lee <[email protected]> wrote:

> I have an idea.
>
>    1. `bulk_create` all companies.
>    2. `Filter`(SELECT) all `Company` and make it dictionary for
>    retrieving company fast.
>    3.
>       1. company_dict = {
>         ’Name’: ‘pk’,
>         ...
>       }
>       4. Create a list of `CompanyValue` with company name and
>    company_dict.
>    5. `bulk_create` the list.
>
>
> I'm not sure if it will work because I haven't implemented it, but I hope
> it helps.
>
> 2019년 11월 29일 금요일 오후 9시 42분 0초 UTC+9, Maxim Bulatov 님의 말:
>>
>> Hello,
>>
>> I have lot of lists like follow:
>> (Name1): (V1, V2, V3, V4, ..)
>>
>> And I use many threads to create many db objects:
>> company = Company(Name)
>> for x in vector:
>>     v = CompanyValue(company=company, value=x)
>>     values.append(v)
>>
>> class CompanyValue(models.Model):
>>     company = models.ForeignKey('Company', on_delete=models.CASCADE)
>>     value = FloatField
>>
>> This pool of threads return all lists, what I combine, filter and want
>> put to database in one bulk_create call.
>> Suddenly, I found, company.id is not ready to be linked in CompanyValue,
>> follow code does not works:
>> Company.objects.bulk_create(companies)
>> CompanyValue.objects.bulk_create(values)      # value.company_id is null
>> here
>>
>> No way to save objects in my threads, because I need to filter objects
>> and can do it only when all of them are collected. Also, it has performance
>> issues 100k+ of requests is slowly enough. I have two ways to resolve:
>> 1. I can make intermediate class or tuple and create CompanyValue objects
>> only when companies is saved to db. Hard to support, hard to filter, not so
>> clean.
>> 2. I can use dirty hack to update _id field:
>>     for v in values:
>>         v.company = v.company
>>
>> Can you advice more ways for me? It seems, I don't know something from
>> django features to make it elegant.
>> I see 11 years old issue here (https://code.djangoproject.com/ticket/9553
>> <https://code.djangoproject.com/ticket/9553#no1>) and it marked as
>> intended behaviour.
>>
> --
> 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/af2a0f27-7234-4210-8627-1ad8982da805%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/af2a0f27-7234-4210-8627-1ad8982da805%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
dvenum
[email protected]

-- 
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/CAHBRYSd1MC5mL3oWTfzqc%2BXXcWxVnDqLpJ5HsEGk2GHFd917eQ%40mail.gmail.com.

Reply via email to