DungeonCoder
DungeonCoder

Reputation: 59

Django REST Framework Serializer: Can't import model

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

Upvotes: 1

Views: 743

Answers (1)

Shreyash mishra
Shreyash mishra

Reputation: 790

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

Upvotes: 1

Related Questions