gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:54:14 25 4
gpt4 key购买 nike

也许我已经筋疲力尽,看不到简单的东西,但是在 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']

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

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

然后有异常(exception),但仅限于某些字段。

来自调试(由 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

关于python - Django - 进行迁移时模型的不可用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44907306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com