Django-迁移时模型的不可用字段

Sła*_*art 7 python migration django

也许我筋疲力尽,看不到简单的东西,但是在Django 1.9.7中,在进行迁移时,我发现了一些奇怪的东西,并且我正在寻找解释。

在RunPython操作中通过apps(是(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']
Run Code Online (Sandbox Code Playgroud)

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

Weight = apps.get_model('calc.Weight')
Run Code Online (Sandbox Code Playgroud)

然后有例外,但仅适用于某些领域。

从调试(由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'
Run Code Online (Sandbox Code Playgroud)

但:

>>> 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'>
Run Code Online (Sandbox Code Playgroud)

那么为什么只有载波域可用,为什么呢?

  • 语法和名称都可以,
  • 准备好的迁移(由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)
    ]
Run Code Online (Sandbox Code Playgroud)

顺便说一句:毕竟,我可以将INF放在课堂之外,并有解决方法,但是了解发生的事情对我来说更重要。

Die*_*goG 7

app.get_model(...)调用将返回一个django.db.models.base.ModelBase实例,而不是您的Weight模型,这就是您看不到INF.

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

  from myapp.models import Weight as WeightModel
  ...
  ...
  maximum = WeightModel.INF
Run Code Online (Sandbox Code Playgroud)