【问题标题】:Django REST Framework Serializer: Can't import modelDjango REST Framework Serializer:无法导入模型
【发布时间】:2022-01-07 19:56:16
【问题描述】:

我有一个模型,我正在尝试制作一个序列化程序,但是我无法将模型导入 serializers.py。当我这样做时,我会收到此错误:

ModuleNotFoundError:没有名为“airquality.air_quality”的模块

这是我要导入的模型

class Microcontrollers(models.Model):
    name = models.CharField(max_length=25)
    serial_number = models.CharField(max_length=20, blank=True, null=True)
    type = models.CharField(max_length=15, blank=True, null=True)
    software = models.CharField(max_length=20, blank=True, null=True)
    version = models.CharField(max_length=5, blank=True, null=True)
    date_installed = models.DateField(blank=True, null=True)
    date_battery_last_replaced = models.DateField(blank=True, null=True)
    source = models.CharField(max_length=10, blank=True, null=True)
    friendly_name = models.CharField(max_length=45, blank=True, null=True)
    private = models.BooleanField()

    class Meta:
        managed = True
        db_table = 'microcontrollers'
        verbose_name_plural = 'Microcontrollers'

    def __str__(self):
        return self.friendly_name

序列化器.py

from rest_framework import serializers

from airquality.air_quality.models import Microcontrollers


class SourceStationsSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        pass

    def update(self, instance, validated_data):
        pass

    class Meta:
        model = Microcontrollers
        fields = ['name']

当我在序列化程序中键入 model = Microcontrollers 时,IDE 本身建议我使用导入

【问题讨论】:

    标签: python django django-models django-rest-framework django-serializer


    【解决方案1】:

    如果模型在同一目录中,则使用此文件

    from .models import Microcontrollers
    

    或者如果它是另一个目录想要导入该模型

    from directoryname.models import Microcontrollers
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      相关资源
      最近更新 更多