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

    1. <small id='NJ7eh'></small><noframes id='NJ7eh'>

        <bdo id='NJ7eh'></bdo><ul id='NJ7eh'></ul>
      <legend id='NJ7eh'><style id='NJ7eh'><dir id='NJ7eh'><q id='NJ7eh'></q></dir></style></legend>
    2. 如何克隆 Django 模型实例对象并将其保存到数据库中?

      How do I clone a Django model instance object and save it to the database?(如何克隆 Django 模型实例对象并将其保存到数据库中?)
        • <small id='5GRtM'></small><noframes id='5GRtM'>

            <bdo id='5GRtM'></bdo><ul id='5GRtM'></ul>

              <tfoot id='5GRtM'></tfoot><legend id='5GRtM'><style id='5GRtM'><dir id='5GRtM'><q id='5GRtM'></q></dir></style></legend>
              <i id='5GRtM'><tr id='5GRtM'><dt id='5GRtM'><q id='5GRtM'><span id='5GRtM'><b id='5GRtM'><form id='5GRtM'><ins id='5GRtM'></ins><ul id='5GRtM'></ul><sub id='5GRtM'></sub></form><legend id='5GRtM'></legend><bdo id='5GRtM'><pre id='5GRtM'><center id='5GRtM'></center></pre></bdo></b><th id='5GRtM'></th></span></q></dt></tr></i><div id='5GRtM'><tfoot id='5GRtM'></tfoot><dl id='5GRtM'><fieldset id='5GRtM'></fieldset></dl></div>

                <tbody id='5GRtM'></tbody>

                本文介绍了如何克隆 Django 模型实例对象并将其保存到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                Foo.objects.get(pk="foo")
                <Foo: test>
                

                在数据库中,我想添加另一个对象,它是上面对象的副本.

                In the database, I want to add another object which is a copy of the object above.

                假设我的桌子只有一行.我想将第一行对象插入具有不同主键的另一行.我该怎么做?

                Suppose my table has one row. I want to insert the first row object into another row with a different primary key. How can I do that?

                推荐答案

                只需更改对象的主键并运行 save().

                Just change the primary key of your object and run save().

                obj = Foo.objects.get(pk=<some_existing_pk>)
                obj.pk = None
                obj.save()
                

                如果您想要自动生成的密钥,请将新密钥设置为无.

                If you want auto-generated key, set the new key to None.

                更多关于更新/插入 这里.

                More on UPDATE/INSERT here.

                关于复制模型实例的官方文档:https://docs.djangoproject.com/en/2.2/topics/db/queries/#copying-model-instances

                Official docs on copying model instances: https://docs.djangoproject.com/en/2.2/topics/db/queries/#copying-model-instances

                这篇关于如何克隆 Django 模型实例对象并将其保存到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                <i id='tmvKg'><tr id='tmvKg'><dt id='tmvKg'><q id='tmvKg'><span id='tmvKg'><b id='tmvKg'><form id='tmvKg'><ins id='tmvKg'></ins><ul id='tmvKg'></ul><sub id='tmvKg'></sub></form><legend id='tmvKg'></legend><bdo id='tmvKg'><pre id='tmvKg'><center id='tmvKg'></center></pre></bdo></b><th id='tmvKg'></th></span></q></dt></tr></i><div id='tmvKg'><tfoot id='tmvKg'></tfoot><dl id='tmvKg'><fieldset id='tmvKg'></fieldset></dl></div>

                      1. <small id='tmvKg'></small><noframes id='tmvKg'>

                        <tfoot id='tmvKg'></tfoot>
                          <bdo id='tmvKg'></bdo><ul id='tmvKg'></ul>
                          <legend id='tmvKg'><style id='tmvKg'><dir id='tmvKg'><q id='tmvKg'></q></dir></style></legend>
                            <tbody id='tmvKg'></tbody>