<small id='a6qok'></small><noframes id='a6qok'>

  1. <i id='a6qok'><tr id='a6qok'><dt id='a6qok'><q id='a6qok'><span id='a6qok'><b id='a6qok'><form id='a6qok'><ins id='a6qok'></ins><ul id='a6qok'></ul><sub id='a6qok'></sub></form><legend id='a6qok'></legend><bdo id='a6qok'><pre id='a6qok'><center id='a6qok'></center></pre></bdo></b><th id='a6qok'></th></span></q></dt></tr></i><div id='a6qok'><tfoot id='a6qok'></tfoot><dl id='a6qok'><fieldset id='a6qok'></fieldset></dl></div>

    • <bdo id='a6qok'></bdo><ul id='a6qok'></ul>
  2. <tfoot id='a6qok'></tfoot>
  3. <legend id='a6qok'><style id='a6qok'><dir id='a6qok'><q id='a6qok'></q></dir></style></legend>

    1. 为ManyToMany字段批量创建的正确方式,Django?

      时间:2024-08-21
        1. <tfoot id='1YOVY'></tfoot>

          <small id='1YOVY'></small><noframes id='1YOVY'>

              <legend id='1YOVY'><style id='1YOVY'><dir id='1YOVY'><q id='1YOVY'></q></dir></style></legend>
                <bdo id='1YOVY'></bdo><ul id='1YOVY'></ul>

                  <tbody id='1YOVY'></tbody>
                <i id='1YOVY'><tr id='1YOVY'><dt id='1YOVY'><q id='1YOVY'><span id='1YOVY'><b id='1YOVY'><form id='1YOVY'><ins id='1YOVY'></ins><ul id='1YOVY'></ul><sub id='1YOVY'></sub></form><legend id='1YOVY'></legend><bdo id='1YOVY'><pre id='1YOVY'><center id='1YOVY'></center></pre></bdo></b><th id='1YOVY'></th></span></q></dt></tr></i><div id='1YOVY'><tfoot id='1YOVY'></tfoot><dl id='1YOVY'><fieldset id='1YOVY'></fieldset></dl></div>
              • 本文介绍了为ManyToMany字段批量创建的正确方式,Django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有用于填充表的代码。

                def add_tags(count):
                    print "Add tags"
                    insert_list = []
                    photo_pk_lower_bound = Photo.objects.all().order_by("id")[0].pk
                    photo_pk_upper_bound = Photo.objects.all().order_by("-id")[0].pk
                    for i in range(count):
                        t = Tag( tag = 'tag' + str(i) )
                        insert_list.append(t)
                    Tag.objects.bulk_create(insert_list)
                    for i in range(count):
                        random_photo_pk = randint(photo_pk_lower_bound, photo_pk_upper_bound)
                        p = Photo.objects.get( pk = random_photo_pk )
                        t = Tag.objects.get( tag = 'tag' + str(i) )
                        t.photos.add(p)
                

                这是型号:

                class Tag(models.Model):
                    tag = models.CharField(max_length=20,unique=True)
                    photos = models.ManyToManyField(Photo)
                

                因为我理解这个答案:Django: invalid keyword argument for this function我必须首先保存Tag对象(由于ManyToMany字段),然后通过add()将照片附加到它们。但对于较大的count,此过程花费的时间太长。是否有任何方法可以重构此代码以使其更快?

                一般来说,我希望用随机虚拟数据填充标记模型。

                编辑%1(照片模型)

                class Photo(models.Model):
                    photo = models.ImageField(upload_to="images")
                    created_date = models.DateTimeField(auto_now=True)
                    user = models.ForeignKey(User)
                
                    def __unicode__(self):
                       return self.photo.name
                

                推荐答案

                TL;DR 使用模型批量插入M2M关系(&Q;到&Q;)。

                "Tag.photos.through" => Django generated Model with 3 fields [ id, photo, tag ]
                photo_tag_1 = Tag.photos.through(photo_id=1, tag_id=1)
                photo_tag_2 = Tag.photos.through(photo_id=1, tag_id=2)
                Tag.photos.through.objects.bulk_insert([photo_tag_1, photo_tag_2, ...])
                

                这是我所知道的最快的方法,我一直使用它来创建测试数据。我可以在几分钟内生成数百万条记录。

                从Georgy编辑:

                def add_tags(count):
                    Tag.objects.bulk_create([Tag(tag='tag%s' % t) for t in range(count)])
                
                    tag_ids = list(Tag.objects.values_list('id', flat=True))
                    photo_ids = Photo.objects.values_list('id', flat=True)
                    tag_count = len(tag_ids)
                       
                    for photo_id in photo_ids:
                        tag_to_photo_links = []
                        shuffle(tag_ids)
                
                        rand_num_tags = randint(0, tag_count)
                        photo_tags = tag_ids[:rand_num_tags]
                
                        for tag_id in photo_tags:
                            # through is the model generated by django to link m2m between tag and photo
                            photo_tag = Tag.photos.through(tag_id=tag_id, photo_id=photo_id)
                            tag_to_photo_links.append(photo_tag)
                
                        Tag.photos.through.objects.bulk_create(tag_to_photo_links, batch_size=7000)
                

                我创建模型不是为了测试,但是结构就在那里,您可能需要调整一些东西才能使其工作。如果您遇到任何问题,请告诉我。

                [编辑]

                这篇关于为ManyToMany字段批量创建的正确方式,Django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:什么是第一类对象(&Q;)? 下一篇:姜戈过滤多对多包含

                相关文章

                  <small id='jZ286'></small><noframes id='jZ286'>

                  <legend id='jZ286'><style id='jZ286'><dir id='jZ286'><q id='jZ286'></q></dir></style></legend>
                    • <bdo id='jZ286'></bdo><ul id='jZ286'></ul>

                  1. <tfoot id='jZ286'></tfoot>
                    <i id='jZ286'><tr id='jZ286'><dt id='jZ286'><q id='jZ286'><span id='jZ286'><b id='jZ286'><form id='jZ286'><ins id='jZ286'></ins><ul id='jZ286'></ul><sub id='jZ286'></sub></form><legend id='jZ286'></legend><bdo id='jZ286'><pre id='jZ286'><center id='jZ286'></center></pre></bdo></b><th id='jZ286'></th></span></q></dt></tr></i><div id='jZ286'><tfoot id='jZ286'></tfoot><dl id='jZ286'><fieldset id='jZ286'></fieldset></dl></div>