Home > Software engineering >  Django REST Framework Serializer: Can't import model
Django REST Framework Serializer: Can't import model

Time:12-02

I have a model i am trying to make a serializer out of however i cannot import the model into serializers.py. When i do i get this error:

ModuleNotFoundError: No module named 'airquality.air_quality'

This is the model i am trying to import

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

serializers.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']

The import is the one the IDE itself suggests i use when i type model = Microcontrollers in the serializer

CodePudding user response:

use this if the model if the file in same directory

from .models import Microcontrollers

or if it is another directory wana import that model

from directoryname.models import Microcontrollers
  • Related