我有工厂模型和方法 get_plant_measurement_field_details 返回以'测量' 开头的工厂字段em>以便我可以使用其名称和详细名称来创建另一个名为 PlantModelMeasurementRange
的模型class Plant(models.Model):
model = models.ForeignKey(PlantModel, null=True, blank=True)
measurement_outlet_length = models.FloatField(
verbose_name=_('Outlet length in cm'),
blank=True,
null=True
)
measurement_outlet_width = models.FloatField(
verbose_name=_('Outlet width in cm'),
blank=True,
null=True
)
def get_plant_measurement_field_details():
fields = Plant._meta.get_fields()
measurement_field_details = []
for field in fields:
if field.name.startswith('measurement'):
measurement_field_details.append(
{
'name': field.name,
'verbose_name': field.verbose_name
}
)
return measurement_field_details
class PlantModelMeasurementRange(models.Model):
model = models.OneToOneField(PlantModel, on_delete=models.CASCADE, related_name='range')
for field in get_plant_measurement_field_details()[0]:
models.FloatField(
verbose_name='Maximum Value of %s' %
field.get('verbose_name').lower()
).contribute_to_class(
PlantModelMeasurementRange, '%s_max' % field.get('name').lower()
)
但是在runserver上它会出现异常:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
有没有更好的方法来解决这个问题。 感谢