python – Django – 进行迁移时不可用的模型字段

也许我很累,看不到简单的东西,但是在Django 1.9.7中,在进行迁移时我发现了一些奇怪的东西,

我正在寻找解释.

在通过应用程序获取模型类(在RunPython操作中它是(django.db.migrations.state.StateApps)时,我对存在的字段具有AttributeError.

我的模特:

class Weight(models.Model):
    INF = 2**31-1

    minimum = models.PositiveIntegerField()
    maximum = models.PositiveIntegerField()
    carrier = models.ForeignKey(Carrier)

    class Meta:
        ordering = ['carrier__name', 'minimum']

在从RunPython运行的迁移方法中,我有:

Weight = apps.get_model('calc.Weight')

然后有异常,但仅限于某些领域.

来自调试(由RunPython运行的内部方法):

>>> Weight.maximum                                                                                              
Traceback (most recent call last):                                                                                   
  File "<pudb command line>", line 1, in <module>                                                                    
AttributeError: type object 'Weight' has no attribute 'maximum'  


>>> Weight.minimum                                               
Traceback (most recent call last):                                                                                   
  File "<pudb command line>", line 1, in <module>                                                                    
AttributeError: type object 'Weight' has no attribute 'minimum'  

>>> Weight.INF                                                                                                  
Traceback (most recent call last):                                                                                   
  File "<pudb command line>", line 1, in <module>                                                                    
AttributeError: type object 'Weight' has no attribute 'INF'

但:

>>> Weight.carrier                                                                                              
<django.db.models.fields.related_descriptors.ForwardManyToOneDescriptor object at 0x7f8dcca692d0>


>>> Weight._meta.fields
(<django.db.models.fields.AutoField: id>, <django.db.models.fields.PositiveIntegerField: minimum>,
<django.db.models.fields.PositiveIntegerField: maximum>, <django.db.models.fields.related.ForeignKey: carrier>)

type(Weight)
<class 'django.db.models.base.ModelBase'>

所以只有操作符领域可用,为什么?

>语法和名称都可以,
>准备好的迁移(由Django提供)也可以(包含所有字段)

——————–

更新:
我的迁移文件是:

from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion

def add_weights(app, *args):
    Carrier = app.get_model('calc.Carrier')
    Weight = app.get_model('calc.Weight')
    # import pudb;pu.db
    carrier_obj = Carrier.objects.get(name='MainCarrier')
    Weight.objects.create(carrier=carrier_obj, minimum=1, maximum=400)  # OK, yes it works within `create`
    Weight.objects.create(carrier=carrier_obj, minimum=401, maximum=800)  # OK
    Weight.objects.create(carrier=carrier_obj, minimum=800, maximum=Weight.INF) # here is AttributeError


class Migration(migrations.Migration):

    dependencies = [
        ('calc', '0012_auto_20170622_1310'),
    ]

    operations = [
        migrations.CreateModel(
            name='Weight',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('minimum', models.PositiveIntegerField()),
                ('maximum', models.PositiveIntegerField()),
                ('carrier', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='calc.Carrier')),
            ],
            options={
                'ordering': ['carrier__name', 'minimum'],
            },
        ),
        migrations.RunPython(add_weights)
    ]

顺便说一下:毕竟我可以将INF放在课堂体外,并有解决方法,但知识发生的事情对我来说更重要.

最佳答案 app.get_model(…)调用将返回一个django.db.models.base.ModelBase实例,而不是您的Weight模型,这就是您无法看到INF的原因.

使用替代名称导入它(因此它不会影响您的Weight变量),并且您将能够使用它:

  from myapp.models import Weight as WeightModel
  ...
  ...
  maximum = WeightModel.INF
点赞