mardi 12 août 2014

python - Django : accès aux données de profil utilisateur dans une autre application - Stack Overflow


I am currently working on a django application and I am trying to develop a little recommendation system for it. I already extended the User model and made a user profile in an app called profile like so:


from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class UserProfile(models.Model):
user = models.OneToOneField(User)
skills = models.CharField(max_length=150)
skillsNeeded = models.CharField(max_length=150)
bio = models.CharField(max_length=300)
industry = models.CharField(max_length=70)
occupation = models.CharField(max_length=70)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

However now I made a separate app within the same Django project called matches and I was wondering how I could pull the current user's data from their profile as well as getting a list of every other user that is in the database. I am using postgresql as my database management system, any ideas or help would be much appreciated. I would think I would need to find out where the profile data is stored in the database and make manual sql queries. However, I am still relatively new to django and this is my first time using postgre for dbms instead of mysql.




Manual SQL queries are almost never necessary. It sounds to me like you just need to use the data models in their basic form to do queries. For example, the current user is available in your views as request.user, so you could get their profile like so:


UserProfile.objects.get(user = request.user)

And getting all of the users is as easy as:


User.objects.all()

I think there is a slight confusion here. Apps in Django are just a heavy-sounding name for a module. Usually, all of your different "apps" will share the same database and can and should import other apps' models to get their work done.



I am currently working on a django application and I am trying to develop a little recommendation system for it. I already extended the User model and made a user profile in an app called profile like so:


from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class UserProfile(models.Model):
user = models.OneToOneField(User)
skills = models.CharField(max_length=150)
skillsNeeded = models.CharField(max_length=150)
bio = models.CharField(max_length=300)
industry = models.CharField(max_length=70)
occupation = models.CharField(max_length=70)
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

However now I made a separate app within the same Django project called matches and I was wondering how I could pull the current user's data from their profile as well as getting a list of every other user that is in the database. I am using postgresql as my database management system, any ideas or help would be much appreciated. I would think I would need to find out where the profile data is stored in the database and make manual sql queries. However, I am still relatively new to django and this is my first time using postgre for dbms instead of mysql.



Manual SQL queries are almost never necessary. It sounds to me like you just need to use the data models in their basic form to do queries. For example, the current user is available in your views as request.user, so you could get their profile like so:


UserProfile.objects.get(user = request.user)

And getting all of the users is as easy as:


User.objects.all()

I think there is a slight confusion here. Apps in Django are just a heavy-sounding name for a module. Usually, all of your different "apps" will share the same database and can and should import other apps' models to get their work done.


0 commentaires:

Enregistrer un commentaire