我使用了以下代码,但收到了错误:
admin.py
from django.contrib import admin
from .models import profile
class profileAdmin(admin.ModelAdmin):
class Meta:
model = profile
admin.site.register(profile,profileAdmin)
__________________ model.py __________________________
from django.db import models
class profiles(models.Model):
name=models.CharField(max_length=120)
#description=models.TextField(null=True)#null value updated in data base
description=models.TextField(default='description default text')
def _unicode_(self):
#def _str_(self):
return self.name
____________________________ settings.py _____
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
]
错误追溯:
from .models import profile
ImportError: cannot import name 'profile'
有人可以建议如何解决这个问题吗?我使用的是python3.4
答案 0 :(得分:0)
如果您的情况profile
表示models.py中的Profile类,那么它应该是:
from .models import Profile
注意资本'P'。您也可以分享您的models.py,以便我们可以看到可能不正确的内容。
答案 1 :(得分:0)
now after some research work i have the solution of the problem that why someone is not able to import "profile" into admin.py.
____________admin.py (corrected and working for above models.py and settings.py)_____)
from django.contrib import admin
from .models import profiles #
class profileAdmin(admin.ModelAdmin):
class Meta:
model = profiles
admin.site.register(profiles,profileAdmin)
People have misconception that one should use from .models import Profile
instead of from .models import profile
in models.py but it's not like that.
we can use any one of them depends upon what is in settings.py and models.py (in INSTALLED_APPS )
(_____________ in settings.py_________)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles',
]
i had used profiles so in my case
from .models import profiles
worked well.
and 2nd thing is class name is profiles in models.py class profiles(models.Model):
________models.py_______
from django.db import models
class profiles(models.Model):
name=models.CharField(max_length=120)
#description=models.TextField(null=True) null value updated in data base
description=models.TextField(default='description default text')
def _unicode_(self):
#def _str_(self):
return self.name
so class Meta:
model = profiles
so, in _______________admin.py_____________ model=profiles has used which is same as class name in ______________models.py_________
class Meta:
model = profiles
admin.site.register(profiles,profileAdmin)
thanks... to all
regards,
Shubham Kumar(UIETH,Panjab University)
shubh2ai@gmail.com