我有一個模型,我正在嘗試使用它來制作序列化器,但是我無法將模型匯入到 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']
匯入是 IDE 本身建議我在序列化程式中鍵入 model = Microcontrollers 時使用的匯入
uj5u.com熱心網友回復:
如果模型在同一目錄中,則使用此檔案
from .models import Microcontrollers
或者如果它是另一個目錄 wana 匯入該模型
from directoryname.models import Microcontrollers
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370654.html
