Django - How to combine queries in to one and display it in the template?

47 views
Skip to first unread message

Robin Lery

unread,
Nov 3, 2013, 8:01:42 AM11/3/13
to django...@googlegroups.com

Suppose this is a model for an app Blog:

class Blog(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)
    creator = models.ForeignKey(User)
    content = BleachField()

And this is another model for an app Status:

class Status(models.Model):
    content = BleachField()
    pub_date = models.DateTimeField(default=datetime.now)
    creator = models.ForeignKey(User)

How do I come up with something like this:

from blog.models import Blog
from status.models import Status

blog = Blog.objecs.all()
status = Status.objects.all()
all = blog | status

And display all the updates for the user in the template.

updates = user.all_set.all()

So that in the templates I can do, something like this:

{% for each in all %}
    ....
{% endfor %}

What I wan't is that, in the home page, I would like to display the updates of all the user's activity in the latest order,

Eg:

'User updated with new status: blah blah'
'User published a new Blog'
'User published a new Blog'

like in many other social networking sites. And not to display them both separately. And similarly in the user's profile, display all the activities of that particular User.

Johannes

unread,
Nov 3, 2013, 8:18:22 AM11/3/13
to django...@googlegroups.com
what about deriving the Blog class from the Status class?

| class Status(models.Model):
content = BleachField()
pub_date = models.DateTimeField(default=datetime.now)
creator = models.ForeignKey(User)|

|class Blog(Status):
title= models.CharField(max_length=200)|


bg,
Johannes

On 03.11.2013 14:01, Robin Lery wrote:
> Suppose this is a model for an app Blog:
>
> |class Blog(models.Model):
> title= models.CharField(max_length=200)
> pub_date= models.DateTimeField(default=datetime.now)
> creator= models.ForeignKey(User)
> content= BleachField()|
>
> And this is another model for an app Status:
>
> |class Status(models.Model):
> content= BleachField()
> pub_date= models.DateTimeField(default=datetime.now)
> creator= models.ForeignKey(User)|
>
> How do I come up with something like this:
>
> |from blog.modelsimport Blog
> from status.modelsimport Status
>
> blog= Blog.objecs.all()
> status= Status.objects.all()
> all= blog| status|
>
> And display all the updates for the user in the template.
>
> |updates= user.all_set.all()|
>
> So that in the templates I can do, something like this:
>
> |{% for eachin all%}
> ....
> {% endfor%}|
>
> What I wan't is that, in the home page, I would like to display the
> updates of all the user's activity in the latest order,
>
> Eg:
>
> |'User updated with new status: blah blah'
> 'User published a new Blog'
> 'User published a new Blog'|
>
> like in many other social networking sites. And not to display them both
> separately. And similarly in the user's profile, display all the
> activities of that particular User.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2B4-nGoMQWG0dhwVTbyJnW6O%2BZaJr4Qvrci1aMGYpZrtbTUukw%40mail.gmail.com.
> For more options, visit https://groups.google.com/groups/opt_out.

pa xapy

unread,
Nov 3, 2013, 8:20:21 AM11/3/13
to django...@googlegroups.com
write custom method on User model or custom template tag
first one allow you to do something like:
{% for item in user.get_what_you_need %}
...
{% endfor %}
and second one:
{% get_what_you_need user %} 
 

Arnold Krille

unread,
Nov 3, 2013, 1:58:16 PM11/3/13
to django...@googlegroups.com
On Sun, 3 Nov 2013 18:31:42 +0530 Robin Lery <robi...@gmail.com>
wrote:
> Suppose this is a model for an app Blog:
> class Blog(models.Model):
<snip>
> And this is another model for an app Status:
> class Status(models.Model):
<snip>
> How do I come up with something like this:
<snip>
> What I wan't is that, in the home page, I would like to display the
> updates of all the user's activity in the latest order,
> Eg:
> 'User updated with new status: blah blah''User published a new
> Blog''User published a new Blog'
> like in many other social networking sites. And not to display them
> both separately. And similarly in the user's profile, display all the
> activities of that particular User.

There are two (actually three) ways to get something like that:
1. Derive Blog and Status from the same concrete basemodel. Then you
can get all the basemodel-instances ordered by time. Of course the
publishing-time (and author and such things) would have to be fields
on the basemodel or present on all derived classes.
Advantage: You get the ordering and limiting done in the database.
Disadvantage: A lot of joins in the database.
2. You do the ordering in the python-code in the view.
Advantage: No concrete inheritance and thus no joins.
Disadvantage: No sorting and limiting in the db (which is faster
with that than your python-code will be).
3. ( Use a schema-free database, called NO-SQL in newer times, and
connect the entries freely. )

Whether to choose 1. or 2. depends on what you do more: Do you fetch
mixed lists and want them ordered? Or do you mainly fetch lists of
specific types and only want the ordering in one or two places? Will
the joins in database-space or the ordering in python-space hurt you
more.

Have fun,

Arnold
signature.asc
Reply all
Reply to author
Forward
0 new messages