简体   繁体   中英

ImportError: cannot import name 'fields' from 'django.db.models.fields'

I got the following error during creation of django serializer.

ImportError: cannot import name 'fields' from 'django.db.models.fields' (/home/user_name/anaconda3/lib/python3.7/site-packages/django/db/models/fields/__init__.py)

and the serializer.py file is

from django.db.models.base import Model
from django.db.models.fields import fields, files
from rest_framework import serializers
from .models import Lead


#create serializers for Lead

class LeadSerializer(serializers.ModelSerializer):
    class Meta:
        model = Lead
        fields = '__all__'

my current django version is 3.0.7. what is wrong in this code?

I'm not sure why you are trying to import fields or files as a matter of fact.

You are trying to import fields from fields . That will not work.

Also, the class Meta doesn't need you to import fields as it has access to this already. Remove the second line of code and give it a go, or if you need files for some other part of your code then just remove fields .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM