django 모델 작업

19712 단어
모델 조작 방법
# -*- coding: utf-8 -*-
# @Time    : 2019-06-16 20:02
# @Author  : songmz
# @email  : [email protected]
import os
import django

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
django.setup()
from django.http import JsonResponse
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


#    
def one_to_one_add():
    """
           ,     
    :param request:
    :return:
    """
    color_obj = Colors.objects.create(colors=u" ")
    print (Ball.objects.create(color=color_obj, description=u"  "))


def one_to_one_search():
    """
             
    :param request:
    :return:
    """
    print Colors.objects.get(ball__description=u'  ').colors


def one_to_one_update():
    """
              
    :param request:
    :return:
    """
    print Colors.objects.filter(ball__description=u'  ').update(colors=u" ")


def one_to_one_delete():
    """
            
    :param request:
    :return:
    """
    print Colors.objects.filter(colors=u" ").delete()


#    
def one_to_many_add():
    """
             ---   
    :param request:
    :return:
    """
    color_obj = Colors.objects.create(colors=u" ")
    print (Clothes.objects.create(description=u"   ", color=color_obj))


def one_to_many_search():
    """
               
    :param request:
    :return:
    """
    print Clothes.objects.filter(color__colors=u' ')


def one_to_many_update():
    """
             ,     "  "
    :param request:
    :return:
    """
    print Clothes.objects.filter(color__colors=u' ').update(description=u'  ')


def one_to_many_delete():
    """
             
    :param request:
    :return:
    """
    print Clothes.objects.filter(color__colors=u' ').delete()


#    
def many_to_many_add():
    """
          、 、 、 
         "  "       
    :param request:
    :return:
    """
    #     
    colors = [" ", " ", " ", " "]
    color_obj = [Colors(colors=i) for i in colors]
    Colors.objects.bulk_create(color_obj)
    #     ,          
    child_obj = Child.objects.create(name=u'  ')
    color_obj = Colors.objects.all()
    child_obj.favor.add(*color_obj)  # .add(*QuerySet  )              *  


def many_to_many_search():
    """
              
             
    :param request:
    :return:
    """
    print Colors.objects.filter(child__name=u'  ')  #     .filter(      __     ="    ")
    print Child.objects.filter(favor__colors=u' ')  #   :filter(      __    ='    ')


def many_to_many_update():
    """
              
    :param request:
    :return:
    """
    child_obj = Child.objects.get(name=u'  ')
    color_obj = Colors.objects.filter(colors__in=[" ", " "])
    child_obj.favor.clear()  #            
    child_obj.favor.add(*color_obj)


def many_to_many_relation():
    """         
             
              
    :param request:
    :return:
    """
    #          
    colors_obj = Colors.objects.get(colors=u' ')
    colors_obj.child_set.clear()  #       "     _set"         .add(),.remove(),.update(),.delete(),.clear()
    #           
    child_obj = Child.objects.get(name=u"  ")
    child_obj.favor.clear()


def many_to_many_data():
    """
        
               
        child
    :return:
    """
    #           ,           
    child_obj = Child.objects.get(name=u'  ')
    color_obj = Colors.objects.all()
    child_obj.favor.add(*color_obj)
    child_obj = Child.objects.create(name=u'  ')
    color_obj = Colors.objects.get(colors=u' ')
    child_obj.favor.add(color_obj)
    #            
    colors_obj = Colors.objects.get(colors=" ")
    colors_obj.child_set.all().delete()  #    .all()
    #     child
    Child.objects.all().delete()


if __name__ == '__main__':
    #    
    # one_to_one_add()
    # one_to_one_search()
    # one_to_one_update()
    # one_to_one_delete()
    #    
    # one_to_many_add()
    # one_to_many_search()
    # one_to_many_update()
    one_to_many_delete()

    #    

좋은 웹페이지 즐겨찾기