Django 数据关系的处理
来源:互联网 发布:黎明杀机淘宝好便宜 编辑:程序博客网 时间:2024/05/21 11:35
Django 数据关系的处理:OneToMany,OneToOne,MangToMany
one-to-many 类型:
>>> e = Entry.objects.get(id=2)>>> e.blog # Returns the related Blog object.
>>> e = Entry.objects.get(id=2)
>>> e.blog = some_blog
>>> e.save()
>>> e = Entry.objects.get(id=2)
>>> e.blog = None
>>> e.save() # "UPDATE blog_entry SET blog_id = NULL ...;"
>>> 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.
>>> 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
>>> 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()
>>> 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()
You cannot access a reverse ForeignKey Manager from the class; it must be accessed from an instance:
>>> Blog.entry_set
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.
many-to-many类型:
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.
one-to-one 类型:
class EntryDetail(models.Model):
entry = models.OneToOneField(Entry)
details = models.TextField()
ed = EntryDetail.objects.get(id=2)
ed.entry # Returns the related Entry object
使用sql语句进行查询:
def my_custom_sql(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
row = cursor.fetchone()
return row
- Django 数据关系的处理
- Django 数据分页处理
- Django数据库一对一数据关系
- django中在数据中删除了关系表后怎么处理
- MySQL的数据关系处理流程
- Django - ORM的继承关系
- Django 的 model 处理,它是一种 ORM (Object Relation Mapping, 对象与关系的映射)
- Django REST framework 嵌套关系的数据保存(Writable nested serializers)
- 联机分析处理(OLAP)与数据挖掘的关系
- sql视图处理复杂的数据查询关系
- 使用 ibatis 处理复杂对象数据关系的实例
- 使用 ibatis 处理复杂对象数据关系的实例
- 使用 ibatis 处理复杂对象数据关系的实例
- 关系型数据的分布式处理系统MyCAT
- 使用 ibatis 处理复杂对象数据关系的实例
- 关系型数据的分布式处理系统MyCAT
- 关系型数据的分布式处理系统MyCAT
- 关系型数据的分布式处理系统MyCAT
- Palindrome Partitioning 分割字符串为回文@LeetCode
- win2008 只显示一个用户
- leetCode 之 Subsets
- Android 漏洞提升权利的
- 使用Beaglebone Black的I2C(一)
- Django 数据关系的处理
- Android Fragment应用实战:点击底部栏不懂选项弹不同Fragment实例
- C++ Map基本操作
- 让学习成为习惯,将分享变成快乐
- MyBatis模糊查询的三种拼接方式
- 结构化异常处理SEH之__finally终止处理
- android 简单易用的ListView 实现多选的解决方案
- Android解析带转义符的JSON
- LeetCode (1) Two Sum (15)Three sum (16)Three sum closest (17)Four sum