U E D R , A S I H C RSS

django/Modifying Object

insert & update

SQLλ¬Έμ—μ„œλŠ” insert into values ꡬ문을 μ΄μš©ν•΄ λ ˆμ½”λ“œλΌ μ‚½μž…ν•˜κ³ , update set where ꡬ문을 μ΄μš©ν•΄ λ ˆμ½”λ“œλΌ μˆ˜μ •ν•œλ‹€. ν•˜μ§€λ§Œ djangoλŠ” 이 λ‘˜μ„ ν•˜λ‚˜λ‘œ 보고 λ°μ΄ν„°λ² μ΄μŠ€μ— λ ˆμ½”λ“œλΌ μ‚½μž…ν•˜κ³  κ°±μ‹ ν•˜λŠ” μž‘μ—…μ„, λͺ¨λΈλ‘œ λ§Œλ“  κ°μ²΄λΌ μ €μž₯(save)ν•˜λŠ” κ²ƒμœΌλ‘œ μΆ”μƒν™”ν–ˆλ‹€. 기본적으둜 λͺ¨λΈν΄λž˜μŠ€λŠ” saveλ©”μ†Œλ“œλΌ κ°€μ§„λ‹€. λ”°λΌμ„œ κ°œλ°œμžκ°€ μž‘μ„±ν•œ λͺ¨λΈλ„ saveλ©”μ†Œλ“œλΌ κ°€μ§€λ©°, μ΄λŠ” μ˜€λ²„λΌμ΄λ”© ν•  수 μžˆλ‹€. μ•„λž˜ μ˜ˆμ—μ„œ 보λ“이 save λ©”μ†Œλ“œλŠ” μƒˆλ‘œλ§Œλ“  λ ˆμ½”λ“œ ν•„λ“œμ˜ 속성에 λ”°λΌμ„œ μ λ‹Ήνžˆ μ‚½μž…κ³Ό κ°±μ‹  μž‘μ—…μ„ μˆ˜ν–‰ν•œλ‹€.
class Employee(models.Model):
    name= models.CharField(maxlength=100)
    email= models.EmailField()
Employee λͺ¨λΈμ— ν•΄λ‹Ήν•˜λŠ” μƒˆλ‘œμš΄ κ°μ²΄λΌ λ§Œλ“€κ³  saveλ©”μ†Œλ“œλΌ μ΄μš©ν•˜λ©΄, λ°μ΄ν„°λ² μ΄μŠ€μ— μƒˆλ‘œμš΄ λ ˆμ½”λ“œλΌ μ‚½μž…ν•˜κ±°λ‚˜, 기쑴의 λ ˆμ½”λ“œλΌ κ°±μ‹ ν•œλ‹€. 기쑴에 μ‚½μž…ν•˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ— 처음 saveλΌ ν˜ΈμΆœν•˜λ©΄ λ ˆμ½”λ“œλΌ μ‚½μž…ν•˜κ³ , λ‹€μŒ 번 saveλΌ ν˜ΈμΆœν•˜λ©΄ λ ˆμ½”λ“œλΌ κ°±μ‹ ν•œλ‹€. λ ˆμ½”λ“œλŠ” 객체둜, λ ˆμ½”λ“œμ˜ 속성을 객체의 멀버 λ³€μˆ˜λ‘œ μ·¨κΈ‰ν•œλ‹€.
e= Employee(name='John', email='John@django.com')
e.save() # insert
e.name= 'jack'
e.save() # update
Screenshot-save.png
saveλ©”μ†Œλ“œλŠ” μš°μ„  ν˜„μž¬ μ €μž₯ν•˜λ €λŠ” λ ˆμ½”λ“œμ˜ μ£Όν‚€λΌ κ°€μ§€κ³  λ°μ΄ν„°λ² μ΄μŠ€λΌ κ²€μƒ‰ν•˜μ—¬ λ ˆμ½”λ“œλΌ μ‚½μž…ν•  지 κ°±μ‹ ν•  지 κ²°μ •ν•œλ‹€. μ£Όν‚€λΌ μ΄μš©ν•΄ 기쑴의 λ ˆμ½”λ“œλΌ μ°Ύμ€ κ²½μš°μ—λŠ” λ‹¨μˆœνžˆ κ°±μ‹ ν•œλ‹€. μ£Όν‚€λΌ μ΄μš©ν•΄ λ ˆμ½”λ“œλΌ μ°Ύμ§€ λͺ»ν•œ κ²½μš°μ—λŠ” λ‹¨μˆœνžˆ μ‚½μž…ν•œλ‹€. 만일 μ£Όν‚€κ°€ μ—†λŠ”λ° λ ˆμ½”λ“œκ°€ μžˆλŠ” κ²½μš°μ—λŠ” μ‚½μž…ν•˜μ—¬λ„ 상관이 μ—†μœΌλ€λ‘œ μ‚½μž…ν•œλ‹€. λ‹€μŒμ€ saveλ©”μ†Œλ“œμ˜ μ£Όμš” 뢄기점을 보여주고 μžˆλ‹€.
  def save(self):
        # First, try an UPDATE. If that doesn't update anything, do an INSERT.
        record_exists = True
        if pk_set:
            # Determine whether a record with the primary key already exists.
            cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
            # If it does already exist, do an UPDATE.
            if cursor.fetchone():
                    cursor.execute("UPDATE %s SET %s WHERE %s=%%s" % \
            else:
                record_exists = False
        if not pk_set or not record_exists:
            if pk_set:
            # If the PK has been manually set, respect that.
            else:
                # Create a new record with defaults for everything.
                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" %

delete

λ°μ΄ν„°λ² μ΄μŠ€μ—μ„œ λ ˆμ½”λ“œλΌ μ‚­μ œν•˜λŠ” μž‘μ—…μ€ Model클래슀의 deleteλ©”μ†Œλ“œλ‘œ μΆ”μƒν™”ν–ˆλ‹€. ν•˜μ§€λ§Œ λ‚΄λΆ€μ—μ„œ μ‹€μ œλ‘œ λ ˆμ½”λ“œλΌ μ‚­μ œν•˜λŠ” λ©”μ†Œλ“œλŠ” delete_objects이닀.8 delete_objectsλ©”μ†Œλ“œλŠ” μ§€μš°λ €λŠ” λ ˆμ½”λ“œλΌ μ°Έμ‘°ν•˜λŠ” λ‹€λ₯Έ ν…Œμ΄λΈ”μ˜ λ ˆμ½”λ“œκΉŒμ§€ ν•¨κ»˜ μ‚­μ œν•˜κ±°λ‚˜, μ™Έλž˜ν‚€λΌ NULLκ°’μœΌλ‘œ μ„μ •ν•œλ‹€. μ˜ˆλΌ λ“€μ–΄ λ‹€μŒμ€ Riskν…Œμ΄λΈ”μ—μ„œ ν•œ λ ˆμ½”λ“œλΌ μ‚­μ œν•˜λŠ” 경우 μ΄λΌ μ°Έμ‘°ν•˜λŠ” Consequence, Control ν…Œμ΄λΈ”μ˜ λ ˆμ½”λ“œκΉŒμ§€ ν•¨κ»˜ μ‚­μ œν•˜λŠ”μ§€λΌ λ¬»λŠ” μ‚¬μš©μž 화면이닀.
django_delete.jpg
----
django/Example
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:38
Processing time 0.0138 sec