__init__()
方法已经在models.Model类中进行了重写规定了特殊含义,所以在模型类中不能重写对象创建空对象后赋值
u = User() <span class="hljs-comment"># 实例化User模型类</span>
u.username = <span class="hljs-string">'lucky'</span> <span class="hljs-comment"># 添加用户名</span>
u.password = <span class="hljs-number">123456</span> <span class="hljs-comment"># 添加密码</span>
u.sex = <span class="hljs-keyword">True</span> <span class="hljs-comment"># 添加性别</span>
u.age = <span class="hljs-number">18</span> <span class="hljs-comment"># 添加年龄</span>
u.info = <span class="hljs-string">'lucky是一个帅气的boy'</span> <span class="hljs-comment"># 添加个人简介</span>
u.save() <span class="hljs-comment"># 进行保存</span>
问题:代码冗余,不利于复用
传参创建对象
u = User(username=<span class="hljs-string">"lucky"</span>, age=<span class="hljs-number">17</span>, sex=<span class="hljs-keyword">True</span>, info=<span class="hljs-string">"lucky is a good man"</span>)
u.save()
问题:关键字参数冗余,并且不提示有哪些属性
管理器类中定义创建方法
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAppManager</span><span class="hljs-params">(models.Manager)</span>:</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_queryset</span><span class="hljs-params">(self)</span>:</span>
<span class="hljs-keyword">return</span> super().get_queryset().filter(isDelete=<span class="hljs-keyword">False</span>)
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, name, age, sex, info)</span>:</span>
obj = self.model() <span class="hljs-comment"># User()</span>
obj.name = name
obj.age = age
obj.sex = sex
obj.info = info
<span class="hljs-keyword">return</span> obj
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(models.Model)</span>:</span>
<span class="hljs-comment">#自定义管理器对象,</span>
objects = MyAppManager()
username = models.CharField(max_length=<span class="hljs-number">20</span>)
password = models.CharField(max_length=<span class="hljs-number">64</span>)
sex = models.BooleanField(default=<span class="hljs-keyword">True</span>)
age = models.IntegerField(default=<span class="hljs-number">20</span>)
info = models.CharField(max_length=<span class="hljs-number">100</span>,default=<span class="hljs-string">'info'</span>)
icon = models.CharField(max_length=<span class="hljs-number">60</span>,default=<span class="hljs-string">'default.jpg'</span>)
isDelete = models.BooleanField(default=<span class="hljs-keyword">False</span>)
createTime = models.DateTimeField(auto_now_add=<span class="hljs-keyword">True</span>)
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
db_table = <span class="hljs-string">"users"</span>
u = User.objects.create(<span class="hljs-string">"lucky"</span>, <span class="hljs-number">16</span>, <span class="hljs-keyword">False</span>, <span class="hljs-string">"lucky is a good Boy"</span>)
u.save()
在模型类中定义一个类方法用于创建对象
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(models.Model)</span>:</span>
username = models.CharField(max_length=<span class="hljs-number">20</span>)
password = models.CharField(max_length=<span class="hljs-number">64</span>)
sex = models.BooleanField(default=<span class="hljs-keyword">True</span>)
age = models.IntegerField(default=<span class="hljs-number">20</span>)
info = models.CharField(max_length=<span class="hljs-number">100</span>,default=<span class="hljs-string">'info'</span>)
icon = models.CharField(max_length=<span class="hljs-number">60</span>,default=<span class="hljs-string">'default.jpg'</span>)
isDelete = models.BooleanField(default=<span class="hljs-keyword">False</span>)
createTime = models.DateTimeField(auto_now_add=<span class="hljs-keyword">True</span>)
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
db_table = <span class="hljs-string">"users"</span>
<span class="hljs-meta"> @classmethod</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create</span><span class="hljs-params">(self, name, age, sex, info)</span>:</span>
<span class="hljs-keyword">return</span> cls(name=name, age=age, sex=sex, info=info)
u = User.create(<span class="hljs-string">"lucky"</span>, <span class="hljs-number">18</span>, <span class="hljs-keyword">False</span>, <span class="hljs-string">"lucky is a bad man"</span>)
u.save()