Sono novizio di Django. Sto cercando di creare UserProfile. In primo luogo ho creato un modello e il suo gestore in models.py come segue ..Errore Django: la query di corrispondenza del profilo utente non esiste
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 Poi modificati con
AUTH_PROFILE_MODULE = "lib.UserProfile"
dove lib è cartella principale che contiene init .py, modelli .py e tutti.
Quindi ho eliminato tutti gli utenti correnti nella raccolta e quando li ho reindirizzati dal pannello di amministrazione, una nuova raccolta lib_userprofile viene creata automaticamente con i campi menzionati nel modello. Ora mi crearted vista come segue
class CurrentUser(APIView):
authentication_classes = (authentication.TokenAuthentication,)
def get(self,request):
if request.user.is_authenticated():
profile=request.user.get_profile()
return Response(profile)
Ma mi sta dando seguente errore ..
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']
Qualsiasi aiuto sarebbe apprezzato per favore .. ho scoperto molte domande su questo errore, ma non ero in grado di risolvere l'errore ..
Please Help .... – Pawan
'UserProfile' dovrebbe avere la relazione' OneToOne' con 'Utente'. Inoltre, il profilo è effettivamente creato? La creazione nel gestore di segnale avrà esito negativo in quanto non viene fornito alcun nome. Probabilmente, non stai mostrando il codice vero e proprio. – Rohan
Ok, l'ho cambiato in OneToOneField. Quando aggiungo un nuovo utente dal pannello di amministrazione Viene automaticamente creato useprofile con questi campi "_id": ObjectId ("528da840ee4340252254e34b"), "nome": "", "user_id": "528da840ee4340252254e34a" E sto mostrando il codice esatto. Quale codice non stai trovando vero .. – Pawan