创建如果不存在

时间:2012-01-07 00:27:19

标签: django models

我有一个Django应用程序,它从Web API读取数据并将其放入数据库中 有没有办法从模式创建新对象,但如果对象已存在,则防止重复异常?

换句话说,有没有办法保存一个对象,但只要它已经存在就什么都不做?

4 个答案:

答案 0 :(得分:124)

答案 1 :(得分:41)

在Django 1.7中,你也可以这样做:

Model.objects.update_or_create()

答案 2 :(得分:2)

在较新版本的Django中,save()函数默认执行UPDATE或INSERT。请参阅here

答案 3 :(得分:1)

可以使用Model.objects.get_or_create()

示例

Sense = 1.4;
--Rate of FIRE (in seconds)
G36ROF = 10 --600RPM.
HKROF = 11.1 --666RPM.
ROF = {G36ROF, HK417ROF} --Rate of FIRE (seconds)
------------------------------------------------------------------
indexWeapon = 0;
indexPattern = 1;
delay = 0;
-------------------------| Recoil Patterns |-----------------------

G36 = {{x=0.0, y=0.0}, {x=-0.337486, y=0.372398}, {x=-0.308392, y=0.418948}, {x=-0.244386, y=0.474808}, {x=-0.216456, y=0.530667}, {x=-0.097755, y=0.551615}, {x=-0.070663, y=1.130321}, {x=0.201095, y=1.246789}, {x=0.361971, y=1.226679}, {x=0.372026, y=1.136187}, {x=0.33784, y=1.423752}, {x=0.180985, y=2.135629}, {x=-0.108591, y=1.966709}, {x=-0.180985, y=1.846052}, {x=-0.33784, y=1.918446}, {x=-0.422299, y=1.797789}, {x=-0.434365, y=1.615839}, {x=-0.22587, y=1.980705}, {x=-0.034749, y=2.258699}, {x=0.121622, y=2.171826}, {x=0.260619, y=2.154451}, {x=0.750583, y=1.81391}, {x=0.938229, y=1.939007}, {x=0.89653, y=2.022405}, {x=0.87568, y=2.064104}, {x=0.733904, y=2.981484}, {x=0.187646, y=1.563715}, {x=0.250194, y=1.709662}, {x=0.479539, y=2.084954}, {x=0.625486, y=1.959856}, {x=1.09877, y=1.824334}, {x=0.89653, y=1.063326}, {x=1.00078, y=0.979928}, {x=1.18842, y=1.313521}, {x=0.917379, y=1.522016}, {x=0.356527, y=1.636689}, {x=-0.229345, y=1.313521}, {x=-0.938229, y=1.230123}, {x=-1.56372, y=1.125875}, {x=-1.27182, y=0.646336}, {x=-1.49283, y=0.542088}, {x=-2.08962, y=0.864672}, {x=-2.49194, y=0.570443}, {x=-3.75292, y=0.09007}, {x=-3.27855, y=-1.044812}, {x=-2.41938, y=-1.853941}, {x=-1.52619, y=-1.551206}, {x=-2.17669, y=-1.025797}, {x=-2.65206, y=0.400311}, {x=-1.15089, y=1.200934}, {x=0.182642, y=1.81391}, {x=0.600467, y=0.690537}, {x=0.780607, y=0.63049}, {x=0.840654, y=0.63049}, {x=1.14089, y=0.72056}, {x=0.987768, y=1.026798}, {x=0.420327, y=1.501167}, {x=-0.870677, y=1.771377}, {x=-1.86145, y=0.210163}, {x=-1.4111, y=-0.600467}, {x=-1.47114, y=-0.888691}, {x=-1.35105, y=-0.840654}, {x=-1.71133, y=-0.960747}, {x=-1.98154, y=-0.810631}, {x=-2.01156, y=-0.45035}, {x=-1.59724, y=0.231179}, {x=-0.330257, y=0.240187}, {x=-0.240187, y=0.210163}, {x=-0.18014, y=0.27021}, {x=-0.18014, y=0.390303}, {x=-0.09007, y=0.54042}, {x=0.870677, y=1.411097}, {x=2.76215, y=-0.030023}, {x=3.42266, y=-0.600466}, {x=3.72289, y=-0.330256}, {x=2.579, y=0.045036}, {x=1.14089, y=0.330257}, {x=1.17091, y=0.300233}, {x=1.32103, y=0.480373}, {x=1.44112, y=0.600467}, {x=1.23396, y=0.63049}, {x=0.750584, y=0.570443}, {x=0.9007, y=0.690537}, {x=0.960747, y=0.81063}, {x=0.930724, y=0.690537}, {x=1.12888, y=0.726565}, {x=1.45113, y=0.425333}, {x=1.47615, y=0.1251}, {x=1.62626, y=-0.125094}, {x=2.00156, y=-0.225171}, {x=2.45191, y=-0.685528}, {x=1.42611, y=-0.400311}, {x=1.47615, y=-0.47537}, {x=2.00156, y=-0.575447}, {x=2.7021, y=-0.750583}, {x=3.37763, y=-0.825642}}
HK417 = {{x=0.0, y=0.0}, {x=0.178027, y=0.483217}, {x=0.139879, y=0.495934}, {x=0.114446, y=0.534082}, {x=0.076297, y=0.432352}, {x=0.071211, y=0.498477}, {x=0.063581, y=0.559515}, {x=0.076297, y=0.750259}, {x=-0.012716, y=0.737542}, {x=-0.012716, y=0.623096}, {x=-0.10936, y=0.782049}, {x=-0.139879, y=1.068165}, {x=-0.330622, y=1.246192}, {x=-0.546799, y=1.297057}, {x=-0.635812, y=1.22076}, {x=-0.506107, y=1.342836}, {x=0.025432, y=1.487801}, {x=0.635812, y=1.767558}, {x=0.826556, y=1.691261}, {x=0.622588, y=1.648026}, {x=0.209288, y=1.437642}, {x=-0.012716, y=1.500517}, {x=-0.368771, y=1.754842}, {x=-0.661245, y=1.907437}, {x=-1.05545, y=2.174478}, {x=-0.957686, y=1.644364}, {x=-0.835, y=1.626052}, {x=-0.65921, y=1.867762}, {x=-0.505395, y=2.351184}, {x=-0.131842, y=2.439079}, {x=0.380145, y=1.88754}, {x=0.791053, y=1.472237}, {x=1.47224, y=1.560132}, {x=1.40632, y=1.164606}, {x=1.45026, y=0.988816}, {x=1.05254, y=0.786658}, {x=0.747105, y=0.900921}, {x=0.703158, y=0.944868}, {x=0.791053, y=1.142631}, {x=0.900921, y=0.944868}, {x=0.881145, y=0.828408}, {x=1.42829, y=0.622588}, {x=1.64803, y=0.238048}, {x=2.06919, y=0.12818}, {x=1.90439, y=0.311294}, {x=1.17193, y=0.631743}, {x=0.622588, y=0.860636}, {x=0.183114, y=0.897259}, {x=-0.366228, y=1.025439}, {x=-0.824013, y=0.78739}, {x=-1.16461, y=0.633574}, {x=-0.988816, y=0.494408}, {x=-1.02544, y=0.494408}, {x=-1.46491, y=0.640899}, {x=-1.77621, y=0.531031}, {x=-1.46345, y=0.202158}, {x=-1.99961, y=-0.087895}, {x=-2.10947, y=-0.395526}, {x=-3.53337, y=-0.764684}, {x=-1.79305, y=-0.316421}, {x=-2.033, y=-0.192489}, {x=-0.949263, y=0.026368}, {x=-1.68758, y=0.105474}, {x=-3.24332, y=0.210947}, {x=-1.60847, y=-0.052737}, {x=-1.68758, y=-0.282142}, {x=-1.2024, y=-0.253137}, {x=-1.55046, y=-0.411347}, {x=-3.32242, y=-0.885979}, {x=-2.12002, y=-0.537916}, {x=-1.73715, y=-0.249973}, {x=-0.442989, y=0.094926}, {x=-0.29436, y=0.300492}, {x=-0.085855, y=0.4906}, {x=0.239167, y=0.410877}, {x=0.521262, y=0.300492}, {x=0.699399, y=0.127164}, {x=0.750265, y=0.101731}, {x=0.813847, y=0.165313}, {x=0.826563, y=0.330625}, {x=0.434475, y=1.133875}, {x=-0.402854, y=0.769085}, {x=-0.71415, y=0.183115}, {x=-1.09869, y=-0.109869}, {x=-1.04376, y=-0.23805}, {x=-0.9522, y=-0.3644}, {x=-0.842331, y=-0.292985}, {x=-1.66635, y=-0.567658}, {x=-1.04376, y=-0.274673}, {x=-0.695839, y=-0.146492}, {x=-0.988823, y=-0.054935}, {x=-0.274673, y=0.201427}, {x=-0.091558, y=0.384542}, {x=0.128181, y=0.274673}, {x=0.402854, y=0.091558}, {x=0.4761, y=0.0}, {x=1.55048, y=-0.189854}, {x=2.43646, y=-0.284781}, {x=4.08186, y=-0.221496}, {x=3.22752, y=0.189855}, {x=1.89634, y=0.672401}, {x=0.57132, y=0.351582}, {x=0.593294, y=0.461451}, {x=0.799848, y=0.613071}, {x=0.85698, y=0.527373}, {x=0.997613, y=0.544952}, {x=1.13912, y=0.411351}, {x=1.79096, y=0.272124}, {x=2.66745, y=0.145555}, {x=2.18016, y=0.072777}, {x=2.38267, y=0.094927}, {x=0.518827, y=0.091558}, {x=0.473048, y=0.137337}, {x=0.671423, y=0.228894}, {x=0.671423, y=0.122077}, {x=1.08801, y=0.193797}, {x=0.747722, y=0.061039}, {x=1.00714, y=0.030519}, {x=0.7935, y=0.030519}, {x=0.991876, y=-0.045779}, {x=0.882006, y=0.033571}}
Wpattern = {G36, HK417}

-----------------------| Weapon compensation |-----------------------
G36Sen = 5.5
HK417Sen = 5 
sensibility = {G36Sen, HK417Sen}

function OnEvent(event, arg)
    EnablePrimaryMouseButtonEvents(true)
    if IsMouseButtonPressed(2) then
        indexWeapon = indexWeapon+1
        if indexWeapon > 2 then
            indexWeapon = 0
        end
        if indexWeapon > 0 then
            if indexWeapon == 1 then
                MoveMouseRelative(0,-10)
            end
            if indexWeapon ==2 then
                MoveMouseRelative(10, 0)
            end
            delay = (1000/ROF[indexWeapon])/5
        end     
    end
    while IsMouseButtonPressed(1) do
        if indexWeapon > 0 then
            if IsMouseButtonPressed(3) and indexPattern < table.getn(Wpattern[indexWeapon]) then            
                MoveMouseRelative(Wpattern[indexWeapon][indexPattern].x*sensibility[indexWeapon]* Sense, Wpattern[indexWeapon][indexPattern].y*sensibility[indexWeapon]*Sense)
                Sleep(delay)
                indexPattern = indexPattern + 1
            else
                Sleep(10)
            end
        end
    end
    indexPattern =1
end

任何传递给 get_or_create() 的关键字参数(这里是 first_namelast_name)——除了一个名为 defaults 的可选参数——将用于在数据库中查询(找到对象)。

它返回一个元组,如果找到一个对象,get_or_create() 返回一个该对象的元组和 False。

注意:同样的事情也可以使用 obj, created = Person.objects.get_or_create( first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)}, ) 语句实现
示例:

try except