2013-11-21 19 views
5

Tôi là người mới đến Django. Tôi đang cố gắng tạo UserProfile. Trước tiên tôi tạo ra một mô hình và xử lý nó trong models.py như sau ..Lỗi Django: truy vấn đối sánh người dùng không tồn tại

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) 

Sau đó, thay đổi nội dung settings.py với

AUTH_PROFILE_MODULE = "lib.UserProfile" 

nơi lib là thư mục gốc chứa init py, các mô hình .py và tất cả.

Sau đó, tôi đã xóa tất cả người dùng hiện tại trong bộ sưu tập và khi tôi nhập lại họ từ bảng quản trị, bộ sưu tập lib_userprofile mới được tạo tự động với các trường tôi đã đề cập trong mô hình. Bây giờ tôi crearted một cái nhìn như sau

class CurrentUser(APIView): 
     authentication_classes = (authentication.TokenAuthentication,) 
     def get(self,request): 
      if request.user.is_authenticated(): 
         profile=request.user.get_profile() 
         return Response(profile) 

Nhưng là đem lại cho tôi sau lỗi ..

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

Bất kỳ trợ giúp sẽ được đánh giá cao xin vui lòng .. tôi phát hiện ra nhiều câu hỏi về lỗi này, nhưng tôi đã không có thể giải quyết lỗi ..

+0

Vui lòng trợ giúp .... – Pawan

+1

'UserProfile' phải có mối quan hệ' OneToOne' với 'Người dùng'. Ngoài ra, hồ sơ có thực sự được tạo ra không? Việc tạo trình xử lý tín hiệu của bạn sẽ thất bại vì không có tên nào được cung cấp. Có lẽ, bạn không hiển thị mã thực. – Rohan

+1

Ok, tôi đã đổi thành OneToOneField. Khi tôi thêm người dùng mới từ bảng quản trị Nó là useprofile được tạo tự động với các trường "_id": ObjectId ("528da840ee4340252254e34b"), "tên": "", "user_id": "528da840ee4340252254e34a" Và tôi đang hiển thị mã chính xác. Mã bạn đang không tìm thấy một thực tế .. – Pawan

Trả lời

8

Tôi đã giải quyết được lỗi. Tôi trả lời câu hỏi của riêng mình vì nó có thể giúp ai đó đối mặt với cùng một vấn đề.

Tôi gặp lỗi trong hàm get_profile, Vì vậy, tôi đã kiểm tra mã của tệp models.py tại số /contrib/auth/models.py tại dòng 383 trong hàm get_profile() bằng cách thay thế user__id__exact=self.id with user = self.id Và nó hoạt động tốt.

+0

Vì vậy, bạn đã thay đổi mã django lõi? Tôi hy vọng nó được cố định ngay bây giờ :) – mehmet

Các vấn đề liên quan