Home > Software design >  In Django how to get specific user's data stored in profiles module
In Django how to get specific user's data stored in profiles module

Time:11-18

I am working on a project where I need to get some data into a chart but I don't know how to get the only data belongs to a specific user.

I am the Using original user modell that I extended with a Profile modell. I'm storing in the Profile a column called "csoport". I like to write a query where I get some other data from my "Kapcsolodasok" modell that joined to Profile modell.

I'm not so experienced in Django ORM so I'm using raw query.

HTML (just the query part)

{% for k in kapcsolodasok %}
  { id: "{{ k.user.get_full_name }}", label: "{{ k.user.get_full_name }}", group:{% for cs in csoport %}{{ cs.csoport }}{% endfor %} },
{% endfor %}

models.py

class Profile(models.Model):

def __str__(self):
    return str(self.user)

user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
date = models.DateField(auto_now_add=True, auto_now=False, blank=True)
projekt = models.ForeignKey(Projekt, on_delete=models.CASCADE, default=1)
csoport = models.CharField(max_length=100)


class Kapcsolodasok(models.Model):

def __str__(self):
    return str(self.user_name)

user_name = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
date = models.DateField(auto_now_add=True, auto_now=False, blank=True)
kap_bar_01 = models.TextField(max_length=200)
kap_bar_02 = models.TextField(max_length=200)
...


class Projekt(models.Model):

def __str__(self):
    return str(self.projekt)

projekt = models.TextField(max_length=150)
date = models.DateField(auto_now_add=True, auto_now=False, blank=True)

views.py

@login_required
def project_details_report(request, projekt_id):

    csoport = Profile.objects.raw('SELECT stressz_profile.projekt_id, stressz_profile.id, stressz_profile.id FROM stressz_profile INNER JOIN stressz_projekt ON stressz_projekt.id = stressz_profile.projekt_id WHERE stressz_projekt.id=%s', [projekt_id]) 
    projekt = Projekt.objects.raw('SELECT projekt_id, id FROM stressz_profile WHERE stressz_profile.projekt_id=%s', [projekt_id])
    kapcsolodasok = Profile.objects.raw('SELECT stressz_profile.id, kap_bar_user, kap_bar_01, kap_bar_02, user_name_id, projekt_id FROM stressz_kapcsolodasok INNER JOIN stressz_profile ON user_name_id = stressz_profile.user_id WHERE projekt_id=%s', [projekt_id])


context = {

    'csoport': csoport,
    'projekt': projekt,
    'kapcsolodasok': kapcsolodasok,
    

}

return render(request, 'stressz/szervezeti_diagnozis.html', context)

The query work but it gives all the data of the csoport column. I need just the data from the csoport column that belongs to the specific user like this: user: 1, csoport: orange user: 2, csoport: lemon user: 3, csoport: orange

Thank you for helping me in advance.

CodePudding user response:

You access the Profile of the user with request.user.profile, you thus can print its .csoport with:

group: {{ request.user.profile.csoport }}

or if you want to use the .user of k, you can work with:

{% for k in kapcsolodasok %}
  { id: "{{ k.user.get_full_name }}", label: "{{ k.user.get_full_name }}", group: {{ k.user.profile.csoport }},
{% endfor %}
  • Related