IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

有关django使用的总结

Vimer 2010-08-12 09:23:54 累计浏览 2,505 次
本机暂存

最近在使用django开发的时候,遇到了很多问题,特此记录在此,希望对以后的同学有所帮助。

一.django的ManyToManyField,当关联自身时,实现单向关联。
比如代码如下:

class MManConfType(models.Model):
    linkconftype = models.ManyToManyField('self',null=True,blank=True)

在这种情况下,当类型1关联了类型2的时候,那么类型2也一定关联了类型1,所以linkconftype就会始终是大于两条记录
解决的方法就是,增加symmetrical=False的定义,即:

linkconftype = models.ManyToManyField('self',symmetrical=False,null=True,blank=True)

官网上的解释如下:

ManyToManyField.symmetrical
Only used in the definition of ManyToManyFields on self. Consider the following model:
class Person(models.Model):
friends = models.ManyToManyField("self")
When Django processes this model, it identifies that it has a ManyToManyField on itself, and as a result, it doesn't add a person_set attribute to the Person class. Instead, the ManyToManyField is assumed to be symmetrical -- that is, if I am your friend, then you are my friend.
If you do not want symmetry in many-to-many relationships with self, set symmetrical to False. This will force Django to add the descriptor for the reverse relationship, allowing ManyToManyField relationships to be non-symmetrical.

二.当model中含有ManyToManyField的时候,如果用

m = form.save(commit = False)
m.save()

来保存的话,ManyToManyField是没有办法保存到数据库的。
解决方法就是:

m = form.save(commit = False)
m.save(seqid=t_seqid)
form.save_m2m()

三.当model中含有ManyToManyField的话,对ManyToManyField做操作,必须要在save_m2m之后.
以第一个为例,当需要获取对linkconftype做任何操作的话,如果将MManConfType的save函数重定义,如:

def save(self, force_insert=False, force_update=False, using=None):
    t = self.linkconftype.all()
    super(MManConf,self).save(force_insert,force_update,using)

则在t = self.linkconftype.all()这行调用会报一个如下错误:
instance needs to have a primary key value before a many-to-many relationship can be used.

所以,只能在

super(MManConf,self).save(force_insert,force_update,using)

之后来对linkconftype做操作,所以,如果linkconftype的类型与是否save相关的话,那么可能就不得不save两次了

而且,如果通过如下方法保存的话,

m = form.save(commit = False)
m.save(seqid=t_seqid)
form.save_m2m()

则,必须在save_m2m之后,对linkconftype进行操作,否则,获取的是修改之前的linkconftype数据。
官方链接如下(速度有点慢):
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

四.用urllib模拟post请求
在python中进行get请求是很容易的:

import urllib
data = urllib.urlopen(url).read()

但是偶尔也会有post的时候,可以用如下代码:

post_params={'r':'1'}
data = urllib.urlencode(post_params)
req = urllib2.Request(url)
fd = urllib2.urlopen(req, data)
data = fd.read()

五.用urllib2模拟form表单,上传文件
这个代码是从网上找的,原文链接如下:
http://www.okpython.com/bbs/thread-3930-1-1.html
码就不贴出来了,直接放出文件下载:multipartpost.py

修改
截图处是我做过修改的,因为原来文件如果传输中文utf8格式会失败。(如果文件是gbk的话,就只能改成gbk了)

OK,乱七八糟说了这么多,总算是写完了,就这样吧~~

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,251)
  2. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 107,164)
  3. 程序员技术练级攻略 (累计阅读 35,469)
  4. python实现自动登录discuz论坛 (累计阅读 32,833)
  5. python编程细节──遍历dict的两种方法比较 (累计阅读 20,371)
  6. 每个程序员都应该学习使用Python或Ruby (累计阅读 17,917)
  7. Chrome和goagent的配置方法,你懂的 (累计阅读 16,842)
  8. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 13,742)
  9. 使用python爬虫抓站的一些技巧总结:进阶篇 (累计阅读 13,301)
  10. 我的PHP,Python和Ruby之路 (累计阅读 13,146)