zoukankan      html  css  js  c++  java
  • django models 关系

    1.一对多/多对一

    class Entry(models.Model):
        name=models.CharField(max_length=50)
        def __str__(self):
            return self.name
    
    class Blog(models.Model):
        name=models.CharField(max_length=20)
        entry=models.ForeignKey("Entry",on_delete=True)     #多对一关键字
        def __str__(self):
            return self.name
    
    >>> from fir.models import Entry,Blog
    >>> entry1=Entry(name='entry1')            #create entry one
    >>> entry2=Entry(name='entry2')
    >>> entry3=Entry(name='entry3')
    >>> blog1=Blog(name='blog1',entry='entry1')    #create blog, but failed
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "Z:xxxxxxmodelsase.py", line 477, in __init__
        _setattr(self, field.name, rel_obj)
      File "Z:xxxxxx
    elated_descriptors.py", line 197, in __set__
        self.field.remote_field.model._meta.object_name,
    ValueError: Cannot assign "'entry1'": "Blog.entry" must be a "Entry" instance.             
    >>> e=Entry.objects.all()
    >>> e
    <QuerySet []>
    >>> entry1.save()                  #forgot to save it, now to save it
    >>> entry2.save()
    >>> entry3.save()
    >>> e=Entry.objects.all()
    >>> e                                    #save successfully
    <QuerySet [<Entry: entry1>, <Entry: entry2>, <Entry: entry3>]>
    >>> blog=Blog(name='blog1',entry='entry1')
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "Z:xxxxxxase.py", line 477, in __init__
        _setattr(self, field.name, rel_obj)
      File "Z:xxxxxx
    elated_descriptors.py", line 197, in __set__
        self.field.remote_field.model._meta.object_name,
    ValueError: Cannot assign "'entry1'": "Blog.entry" must be a "Entry" instance.          #must be a instance of Entry, not the entry's name
    >>> blog=Blog(name='blog1',entry=entry1)                   #create successfully
    >>> blog
    <Blog: blog1>
    >>> blog.entry
    <Entry: entry1>
    >>> blog.save() #don't forget to save it
    从多里面查一。
    >>> dir(blog)
    [..., 'entry', 'entry_id', 'id', 'name', 'objects',... ]
    #we can find both 'entry' and 'entry_id', then we can find the entry_id through blog
    >>> a=blog.entry_id
    >>> a
    1
    >>> b=blog.entry
    >>> b
    <Entry: entry1>           #object
    >>>
    
    从一里面查多。
    
    >>> dir(entry1)
    [ 'blog_set', 'check', 'id', 'name', 'objects'...]
    #we can find related blog sets through blog_set
    >>> c=entry1.blog_set
    >>> c
    <django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x0000000003EDEF60>
    >>> d=entry1.blog_set.all()

        >>> entry1.blog_set.all()
       <QuerySet [<Blog: blog1>]>

      

      

    2.manytomany

    3.onetoone

  • 相关阅读:
    zabbix系列(七)zabbix3.0添加对tcp连接数及状态的监控
    zabbix系列(六)zabbix添加对ubuntu系统的监控
    zabbix系列(六)zabbix添加对ubuntu系统的监控
    centos命令行变成了-bash-4.1$的解决办法
    zabbix常见报错问题处理
    sqlyog通过跳板机ssh连接mysql数据库
    centos系统初始化脚本
    修改centos和ubuntu ssh远程连接端口提升系统安全性
    listener failed: zbx_tcp_listen() fatal error: unable to serve on any address [[-]:20050]
    zabbix系列(五)zabbix3.0.4 探索主机Discovery自动发现主机详细图文教程
  • 原文地址:https://www.cnblogs.com/mingxiazhichan/p/9011840.html
Copyright © 2011-2022 走看看