<bdo id='6s28M'></bdo><ul id='6s28M'></ul>
<legend id='6s28M'><style id='6s28M'><dir id='6s28M'><q id='6s28M'></q></dir></style></legend>

    <small id='6s28M'></small><noframes id='6s28M'>

    <tfoot id='6s28M'></tfoot>
    <i id='6s28M'><tr id='6s28M'><dt id='6s28M'><q id='6s28M'><span id='6s28M'><b id='6s28M'><form id='6s28M'><ins id='6s28M'></ins><ul id='6s28M'></ul><sub id='6s28M'></sub></form><legend id='6s28M'></legend><bdo id='6s28M'><pre id='6s28M'><center id='6s28M'></center></pre></bdo></b><th id='6s28M'></th></span></q></dt></tr></i><div id='6s28M'><tfoot id='6s28M'></tfoot><dl id='6s28M'><fieldset id='6s28M'></fieldset></dl></div>

    1. Django 模型与Python 类属性

      时间:2023-09-13

      1. <tfoot id='hE3TD'></tfoot>
        • <legend id='hE3TD'><style id='hE3TD'><dir id='hE3TD'><q id='hE3TD'></q></dir></style></legend>

          <small id='hE3TD'></small><noframes id='hE3TD'>

              <tbody id='hE3TD'></tbody>
            • <bdo id='hE3TD'></bdo><ul id='hE3TD'></ul>
              1. <i id='hE3TD'><tr id='hE3TD'><dt id='hE3TD'><q id='hE3TD'><span id='hE3TD'><b id='hE3TD'><form id='hE3TD'><ins id='hE3TD'></ins><ul id='hE3TD'></ul><sub id='hE3TD'></sub></form><legend id='hE3TD'></legend><bdo id='hE3TD'><pre id='hE3TD'><center id='hE3TD'></center></pre></bdo></b><th id='hE3TD'></th></span></q></dt></tr></i><div id='hE3TD'><tfoot id='hE3TD'></tfoot><dl id='hE3TD'><fieldset id='hE3TD'></fieldset></dl></div>

                本文介绍了Django 模型与Python 类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                django 网站上的教程显示了模型的以下代码:

                The tutorial on the django website shows this code for the models:

                from django.db import models
                
                class Poll(models.Model):
                    question = models.CharField(max_length=200)
                    pub_date = models.DateTimeField('date published')
                
                class Choice(models.Model):
                    poll = models.ForeignKey(Poll)
                    choice = models.CharField(max_length=200)
                    votes = models.IntegerField()
                

                现在,这些属性中的每一个都是类属性,对吧?因此,该类的所有实例都应共享相同的属性.稍后,他们展示了这段代码:

                Now, each of those attribute, is a class attribute, right? So, the same attribute should be shared by all instances of the class. A bit later, they present this code:

                class Poll(models.Model):
                    # ...
                    def __unicode__(self):
                        return self.question
                
                class Choice(models.Model):
                    # ...
                    def __unicode__(self):
                        return self.choice
                

                它们是如何从类属性转变为实例属性的?我是不是把类属性弄错了?

                How did they turn from class attributes into instance attributes? Did I get class attributes wrong?

                推荐答案

                看看django/db/models.py下的Model类.在那里,类属性通过类似的方式转换为实例属性

                Have a look at the Model class under django/db/models.py. There the class attributes are turned to instance attributes via something like

                setattr(self, field.attname, val)
                

                人们可能会推荐整个文件(ModelBase 和 Model 类)作为元类的优秀实践示例.

                One might recommend the whole file (ModelBase and Model class) as an excellent hands-on example on metaclasses.

                这篇关于Django 模型与Python 类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何在 Python 3.5 中键入提示属性? 下一篇:“私人"名称修改和实例与类属性

                相关文章

                最新文章

              2. <small id='rbKbA'></small><noframes id='rbKbA'>

                • <bdo id='rbKbA'></bdo><ul id='rbKbA'></ul>

                <legend id='rbKbA'><style id='rbKbA'><dir id='rbKbA'><q id='rbKbA'></q></dir></style></legend>
                <i id='rbKbA'><tr id='rbKbA'><dt id='rbKbA'><q id='rbKbA'><span id='rbKbA'><b id='rbKbA'><form id='rbKbA'><ins id='rbKbA'></ins><ul id='rbKbA'></ul><sub id='rbKbA'></sub></form><legend id='rbKbA'></legend><bdo id='rbKbA'><pre id='rbKbA'><center id='rbKbA'></center></pre></bdo></b><th id='rbKbA'></th></span></q></dt></tr></i><div id='rbKbA'><tfoot id='rbKbA'></tfoot><dl id='rbKbA'><fieldset id='rbKbA'></fieldset></dl></div>

                    <tfoot id='rbKbA'></tfoot>