snipt

Ctrl+h for KB shortcuts

Python

django: check foreign key beforeSave

#check if foreignKey exixts before saving related object

def save(self, **kwargs):
    super(JobRecord, self).save(**kwargs)

    if self.operators.filter(id=self.supervisor.id).count() == 0:
    #if self.supervisor not in self.operators.values():

        # either conditional will get to this point
        self.operators.add(self.supervisor) # <-- this line doesn't save proper?
https://snipt.net/embed/4d7cf8213b6120a640be595edec3ec07/
/raw/4d7cf8213b6120a640be595edec3ec07/
4d7cf8213b6120a640be595edec3ec07
python
Python
11
2019-08-24T05:03:42
True
False
False
/api/public/snipt/4294/
djangocheck-foreign-key-beforesave
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">#check if foreignKey exixts before saving related object</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> </span><span id="L-4"><a name="L-4"></a> <span class="nb">super</span><span class="p">(</span><span class="n">JobRecord</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">supervisor</span><span class="o">.</span><span class="n">id</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> </span><span id="L-7"><a name="L-7"></a> <span class="c1">#if self.supervisor not in self.operators.values():</span> </span><span id="L-8"><a name="L-8"></a> </span><span id="L-9"><a name="L-9"></a> <span class="c1"># either conditional will get to this point</span> </span><span id="L-10"><a name="L-10"></a> <span class="bp">self</span><span class="o">.</span><span class="n">operators</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">supervisor</span><span class="p">)</span> <span class="c1"># &lt;-- this line doesn&#39;t save proper?</span> </span></pre></div> </td></tr></table>
django, model