我是Django的新手。我正在嘗試創建UserProfile。首先,我創建了一個模型及其在models.py處理程序如下..Django錯誤:userprofile匹配查詢不存在
class UserProfile(models.Model):
user = models.ForeignKey(User,null=False)
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)
然後編輯settings.py與
AUTH_PROFILE_MODULE = "lib.UserProfile"
,其中lib是包含初始化的.py根文件夾,模型.py和所有。
然後,我刪除了所有當前用戶的集合中,當我從管理面板重新進入他們,一個新的集合lib_userprofile自動與我在模型中提到的字段創建。現在我crearted視圖如下
class CurrentUser(APIView):
authentication_classes = (authentication.TokenAuthentication,)
def get(self,request):
if request.user.is_authenticated():
profile=request.user.get_profile()
return Response(profile)
不過是給我下面的錯誤..
UserProfile matching query does not exist.
Request Method: GET
Request URL: http://pawan.demoilab.pune/api/currentuser
Django Version: 1.3.7
Exception Type: DoesNotExist
Exception Value:
UserProfile matching query does not exist.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/models/query.py in get, line 351
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/var/www/pawan.demoilab.pune/web/api',
'/usr/local/lib/python2.7/dist-packages/Fabric-1.8.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/paramiko-1.12.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/ecdsa-0.10-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/pymongo-2.6.3-py2.7-linux-i686.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/pymodules/python2.7']
任何幫助,將不勝感激,請..我發現這個錯誤的許多問題,但我不能夠解決錯誤..
請幫助.... – Pawan
'UserProfile'應該有'和'User' OneToOne'關係。另外,實際創建的配置文件是?由於沒有提供名稱,您在信號處理程序中的創建將失敗。可能你沒有顯示實際的代碼。 – Rohan
好的,我將它改爲OneToOneField。當我從管理面板添加新用戶它的用戶配置文件自動創建這些字段「_id」:ObjectId(「528da840ee4340252254e34b」), 「name」:「」, 「user_id」:「528da840ee4340252254e34a」我顯示了確切的代碼。你目前還沒有找到什麼代碼實際的.. – Pawan