如何在命令行下操作Django构建的数据对象

如题所述

进入命令行界面manage.py shell引入在auth包模型文件中的两个类from auth.models import AreaProvincefrom auth.models import AreaCity注意,命令行不支持中文字符,还没来得及研究创建并保存一个对象p=AreaProvince(Province='Henan')p.save下面是关键,创建一个城市c=AreaCity(Province=p,City='Kaifeng')c.save出错IntegrityError: auth_areacity.Province_id may not be NULL指明这个对象的Province.id不能为空这个好办p=AreaProvince.ojbects.filter(ID=1)这个对象能用不?测一下p.ID出错AttributeError: 'QuerySet' object has no attribute 'ID'查询结果集不能有属性ID看来不能用filter,就算filter的结果是一条数据也是集合p=AreaProvince.objects.get(ID=1)p.ID1c=AreaCity(Province=p,City='Kaifeng')c.save()成功了!再看一下这个对象
温馨提示:答案为网友推荐,仅供参考
相似回答