系统的了解DJANGO中数据MODULES的相关性引用

数据库结构如下:

from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Author(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()

    def __str__(self):              # __unicode__ on Python 2
        return self.name

class Entry(models.Model):
    blog = models.ForeignKey(Blog)
    headline = models.CharField(max_length=255)
    body_text = models.TextField()
    pub_date = models.DateField()
    mod_date = models.DateField()
    authors = models.ManyToManyField(Author)
    n_comments = models.IntegerField()
    n_pingbacks = models.IntegerField()
    rating = models.IntegerField()

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

 

终于对这个东东有个系统性的了解了。

真正要用时,还是要查一查的。。

Related objects

When you define a relationship in a model (i.e., a ForeignKeyOneToOneField, or ManyToManyField), instances of that model will have a convenient API to access the related object(s).

Using the models at the top of this page, for example, an Entry object e can get its associated Blog object by accessing the blogattribute: e.blog.

(Behind the scenes, this functionality is implemented by Python descriptors. This shouldn’t really matter to you, but we point it out here for the curious.)

Django also creates API accessors for the “other” side of the relationship – the link from the related model to the model that defines the relationship. For example, a Blog object b has access to a list of all related Entry objects via the entry_set attribute:b.entry_set.all().

All examples in this section use the sample Blog, Author and Entry models defined at the top of this page.

One-to-many relationships

Forward

If a model has a ForeignKey, instances of that model will have access to the related (foreign) object via a simple attribute of the model.

Example:

>>> e = Entry.objects.get(id=2)
>>> e.blog # Returns the related Blog object.

You can get and set via a foreign-key attribute. As you may expect, changes to the foreign key aren’t saved to the database until you callsave(). Example:

>>> e = Entry.objects.get(id=2)
>>> e.blog = some_blog
>>> e.save()

If a ForeignKey field has null=True set (i.e., it allows NULL values), you can assign None to remove the relation. Example:

>>> e = Entry.objects.get(id=2)
>>> e.blog = None
>>> e.save() # "UPDATE blog_entry SET blog_id = NULL ...;"

Forward access to one-to-many relationships is cached the first time the related object is accessed. Subsequent accesses to the foreign key on the same object instance are cached. Example:

>>> e = Entry.objects.get(id=2)
>>> print(e.blog)  # Hits the database to retrieve the associated Blog.
>>> print(e.blog)  # Doesn't hit the database; uses cached version.

Note that the select_related() QuerySet method recursively prepopulates the cache of all one-to-many relationships ahead of time. Example:

>>> e = Entry.objects.select_related().get(id=2)
>>> print(e.blog)  # Doesn't hit the database; uses cached version.
>>> print(e.blog)  # Doesn't hit the database; uses cached version.

Following relationships “backward”

If a model has a ForeignKey, instances of the foreign-key model will have access to a Manager that returns all instances of the first model. By default, this Manager is named FOO_set, where FOO is the source model name, lowercased. This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above.

Example:

>>> b = Blog.objects.get(id=1)
>>> b.entry_set.all() # Returns all Entry objects related to Blog.

# b.entry_set is a Manager that returns QuerySets.
>>> b.entry_set.filter(headline__contains='Lennon')
>>> b.entry_set.count()

You can override the FOO_set name by setting the related_name parameter in the ForeignKey definition. For example, if the Entrymodel was altered to blog = ForeignKey(Blog, related_name='entries'), the above example code would look like this:

>>> b = Blog.objects.get(id=1)
>>> b.entries.all() # Returns all Entry objects related to Blog.

# b.entries is a Manager that returns QuerySets.
>>> b.entries.filter(headline__contains='Lennon')
>>> b.entries.count()

Using a custom reverse manager

New in Django 1.7.

By default the RelatedManager used for reverse relations is a subclass of the default manager for that model. If you would like to specify a different manager for a given query you can use the following syntax:

from django.db import models

class Entry(models.Model):
    #...
    objects = models.Manager()  # Default Manager
    entries = EntryManager()    # Custom Manager

b = Blog.objects.get(id=1)
b.entry_set(manager='entries').all()

If EntryManager performed default filtering in its get_queryset() method, that filtering would apply to the all() call.

Of course, specifying a custom reverse manager also enables you to call its custom methods:

b.entry_set(manager='entries').is_published()

Additional methods to handle related objects

In addition to the QuerySet methods defined in “Retrieving objects” above, the ForeignKey Manager has additional methods used to handle the set of related objects. A synopsis of each is below, and complete details can be found in the related objects reference.

add(obj1, obj2, ...)
Adds the specified model objects to the related object set.
create(**kwargs)
Creates a new object, saves it and puts it in the related object set. Returns the newly created object.
remove(obj1, obj2, ...)
Removes the specified model objects from the related object set.
clear()
Removes all objects from the related object set.

To assign the members of a related set in one fell swoop, just assign to it from any iterable object. The iterable can contain object instances, or just a list of primary key values. For example:

b = Blog.objects.get(id=1)
b.entry_set = [e1, e2]

In this example, e1 and e2 can be full Entry instances, or integer primary key values.

If the clear() method is available, any pre-existing objects will be removed from the entry_set before all objects in the iterable (in this case, a list) are added to the set. If the clear() method is not available, all objects in the iterable will be added without removing any existing elements.

Each “reverse” operation described in this section has an immediate effect on the database. Every addition, creation and deletion is immediately and automatically saved to the database.

Many-to-many relationships

Both ends of a many-to-many relationship get automatic API access to the other end. The API works just as a “backward” one-to-many relationship, above.

The only difference is in the attribute naming: The model that defines the ManyToManyField uses the attribute name of that field itself, whereas the “reverse” model uses the lowercased model name of the original model, plus '_set' (just like reverse one-to-many relationships).

An example makes this easier to understand:

e = Entry.objects.get(id=3)
e.authors.all() # Returns all Author objects for this Entry.
e.authors.count()
e.authors.filter(name__contains='John')

a = Author.objects.get(id=5)
a.entry_set.all() # Returns all Entry objects for this Author.

Like ForeignKeyManyToManyField can specify related_name. In the above example, if the ManyToManyField in Entry had specified related_name='entries', then each Author instance would have an entries attribute instead of entry_set.

One-to-one relationships

One-to-one relationships are very similar to many-to-one relationships. If you define a OneToOneField on your model, instances of that model will have access to the related object via a simple attribute of the model.

For example:

class EntryDetail(models.Model):
    entry = models.OneToOneField(Entry)
    details = models.TextField()

ed = EntryDetail.objects.get(id=2)
ed.entry # Returns the related Entry object.

The difference comes in “reverse” queries. The related model in a one-to-one relationship also has access to a Manager object, but thatManager represents a single object, rather than a collection of objects:

e = Entry.objects.get(id=2)
e.entrydetail # returns the related EntryDetail object

If no object has been assigned to this relationship, Django will raise a DoesNotExist exception.

Instances can be assigned to the reverse relationship in the same way as you would assign the forward relationship:

e.entrydetail = ed

How are the backward relationships possible?

Other object-relational mappers require you to define relationships on both sides. The Django developers believe this is a violation of the DRY (Don’t Repeat Yourself) principle, so Django only requires you to define the relationship on one end.

But how is this possible, given that a model class doesn’t know which other model classes are related to it until those other model classes are loaded?

The answer lies in the app registry. When Django starts, it imports each application listed in INSTALLED_APPS, and then the modelsmodule inside each application. Whenever a new model class is created, Django adds backward-relationships to any related models. If the related models haven’t been imported yet, Django keeps tracks of the relationships and adds them when the related models eventually are imported.

For this reason, it’s particularly important that all the models you’re using be defined in applications listed in INSTALLED_APPS. Otherwise, backwards relations may not work properly.

时间: 2024-10-31 16:16:26

系统的了解DJANGO中数据MODULES的相关性引用的相关文章

编程-VFP 更新表数据时自动把系统时间写入表中指定一列插入 如何实现自动写入数据

问题描述 VFP 更新表数据时自动把系统时间写入表中指定一列插入 如何实现自动写入数据 VFP编程 现有 成绩 用户 两表 成绩表 三列 成绩 修改时间 用户 用户表 两列 用户名 密码 其中插入与更新成绩后,自动提取系统时间 写入 "修改时间列"( 包含年月日的时间 ) 并且根据系统登录的用户,将用户名写入成绩表 用户列 修改时间以及 用户 这两列只能通过程序自动写入 不可以人为写入 现有两表 教师 用户 如何实现插入教师表中教师号一列的数据后 自动将数据插入到用户表中 用户名列 用

django中使用jquery ajax post数据出现403错误的解决办法(两种方法)_AJAX相关

在django中,使用jquery ajax post数据,会出现403的错误 方法一: 如果用jQuery来处理ajax的话,Django直接送了一段解决问题的代码.把它放在一个独立的js文件中,在html页面中都引入即可.注意这个js文件必须在jquery的js文件引入之后,再引入即可 $(document).ajaxSend(function(event, xhr, settings) { function getCookie(name) { var cookieValue = null;

django中使用jquery ajax post数据出现403错误的解决办法(两种方法)

在django中,使用jquery ajax post数据,会出现403的错误 方法一: 如果用jQuery来处理ajax的话,Django直接送了一段解决问题的代码.把它放在一个独立的js文件中,在html页面中都引入即可.注意这个js文件必须在jquery的js文件引入之后,再引入即可 $(document).ajaxSend(function(event, xhr, settings) { function getCookie(name) { var cookieValue = null;

asp 读取数据库-asp编写的网页,读取数据库时,找不新建的表中数据,rs.eof 为真(此网页可读取别的表中的数据)

问题描述 asp编写的网页,读取数据库时,找不新建的表中数据,rs.eof 为真(此网页可读取别的表中的数据) <% size=request("size") c1=request("case") if size="" then size=20 end if n=1 title=trim(request("title")) action=request("action") sh=request(&qu

Win7系统如何在excel中添加word文档结构图

  Win7系统如何在excel中添加word文档结构图          方法一: Word提供了一个很方便的功能--文档结构图,通过文档结构图可以在整个文档中快速浏览并追踪特定的文件内容位置.文档结构图是一个独立的窗口,类似于大纲视图,它能够显示文档的标题,单击文档结构图中的标题后,Word就会自动转到文档中的相应标题,并将其显示在窗口的顶部.用户可以单击菜单""视图"-"文档结构图"来打开它.但是EXCEL中没有提供文档结构图,那么如何管理数据量较大

详述如何提高MySQL中数据装载效率

很多时候关心的是优化SELECT 查询,因为它们是最常用的查询,而且确定怎样优化它们并不总是直截了当.相对来说,将数据装入数据库是直截了当的.然而,也存在可用来改善数据装载操作效率的策略,其基本原理如下: 成批装载较单行装载更快,因为在装载每个记录后,不需要刷新索引高速缓存;可在成批记录装入后才刷新. 在表无索引时装载比索引后装载更快.如果有索引,不仅必须增加记录到数据文件,而且还要修改每个索引以反映增加了的新记录. 较短的SQL 语句比较长的SQL 语句要快,因为它们涉及服务器方的分析较少,而

Web环境下中数据的磁带备份与恢复

web|备份|恢复|数据 实现磁带备份数据的功能有两方面的困难:首先,MS SQL Server所提供的数据库的整体备份及恢复功能不能直接满足本系统要求的数据滚动备份.其次,需要解决如何在Web环境下实现磁带数据备份功能. 利用SQL中现有的数据库备份和恢复的命令以及NT中的IDC技术,实现SQL数据库中数据滚动备份到磁带的功能.本系统所实现功能既能保证近一段时间的数据总在数据库中,又能保证系统管理员可随时恢复备份数据,供用户查询.本文所实现的功能具有普遍的意义,特别适用于中小型企业开发基于In

Web环境下MS SQL Server中数据的磁带备份与恢复

server|web|备份|恢复|数据 摘 要:介绍了磁带数据备份及恢复的工作过程,包括在硬盘上建立了一个与磁带的容量相当的数据库,即桥数据库和在Web信息系统中要实现完整的磁带数据备份及恢复功能.从理论与实践上阐述了如何利用SQL中现有的数据库备份和恢复的命令以及NT中的IDC技术. 关键词:Web信息系统:磁带数据备份:桥数据库:IDC文件:数据恢复 1 引言 实现磁带备份数据的功能有两方面的困难:首先,MS SQL Server(以下简称SQL)所提供的数据库的整体备份及恢复功能不能直接满

Remoting中数据序列化

数据 该文讲述通过网络传输序列化数据的两个类,BinaryFormatter和SoapFormatter类.这些类可以将类的实例转化成字节流通过网络传输到远程系统,也可以转换回原来的数据. 一. 使用序列化类 序列化一个类,并通过网络传输需要三步: 1.将要序列化的类创建成一个library对象. 2.编写一个发送程序来创建要序列化类的实例,并发送. 3.编写一个接收程序从流中读取数据,并重新创建原来的序列化类. ① 编写要序列化的类 每个要通过网络传输数据的类必须在原代码文件里使用[Seria