技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 其他 --> 有关django使用的总结

有关django使用的总结

浏览:1921次  出处信息

最近在使用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. 使用django+celery+RabbitMQ实现异步执行    (阅读:4912)
  2. Django 中 "Data truncated for column xxx" 解决方法    (阅读:4218)
  3. apache+mod_wsgi+django在windows下的部署    (阅读:3951)
  4. Django框架ORM操作详解    (阅读:3926)
  5. python-django的中文编码总结    (阅读:3669)
  6. django中动态生成form表单    (阅读:3427)
  7. 回归简单,向Django说再见    (阅读:3428)
  8. 在dotcloud上部署Django全程记录    (阅读:2936)
  9. Django数据库访问优化    (阅读:2735)
  10. Django的静态文件服务 总结    (阅读:2497)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
  • 作者:Dante    来源: Vimer
  • 标签: django
  • 发布时间:2010-08-12 09:23:54
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1