sql - Join in Django? -


i'm trying make application in django, need join in django, somethin that:

select user.name, post.contain user, post post.user_id = user.id; 

hoy can user.name?

my model

from django.db import models django.contrib.auth.models import user   class post(models.model):     titulo = models.charfield(max_length = 60)     contenido = models.textfield(max_length = 140)     fecha = models.datetimefield(auto_now_add = true, blank = true)     usuario = models.foreignkey(user)      def __unicode__(self):         return self.titulo 

really thank =)

most of time, should not worry joins. orms job.

posts = post.objects.filter(user=user) 

after first query, user should in orm cache, don't worry performance unless detect performance bottleneck (premature optimization root of evil).

{% post in posts %}     {% if forloop.first %}<h1>{{ post.user.name }}{% endif %}     <li>{{ post.contain }}</li> {% endfor %} 

if want control freak:

posts = post.objects.select_related().filter(user=user_id)\                                      .values('user__name', 'contain') 

[update]

django.contrib.auth.models.user lacks field called name. has username, first_name , last_name. there get_full_namemethod`suggested karthikr.

what result of following?

post.objects.filter(user=user_id) 

the template should be:

{% post in posts %}     {% if forloop.first %}     <h1>{% trans 'posts for' %}         {{ post.usuario.get_full_name|default:post.usuario.username }}     </h1>     <ul>     {% endif %}       <li>{{ post.contenido }}</li>     {% if forloop.last %}     </ul>     {% endif %} {% endfor %} 

try same queryset without filter see if problem filter (like user having no posts).

post.objects.select_related().filter(usuario=user_id)\                          .values('usuario__username', 'contenido') 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -