views:

39

answers:

1
+1  Q: 

Django 'timeline'

hi there,

i want to make a 'timeline' containing all mini blog posts from a user, and all the user he is following. I want all these posts to be ordered by date..But how can i 'join' the posts, because the 'following' relation is in another table, so i have to make some kind of a join between the two tables, for taking the data.

For now, in my 'timeline', there appears only the blog posts of the owner of the blog,and i user a query like:

     blog = New.objects.filter(created_by = request.user) 

but i want to join there posts with the posts of the persons he is following, meaning:

    following = Relations.objects.filter(initiated_by = request.user)

where Relations is the 'Follow relation' table.

how can i do it? thank you!

my models:

class New(models.Model):
post = models.CharField(max_length=120)
date = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True) 
objects = NewManager()   


class Relations(models.Model):
initiated_by = models.ForeignKey(User, editable=False, related_name = 'initiator')
date_initiated = models.DateTimeField(auto_now=True, editable = False)
follow = models.ForeignKey(User, editable = False, related_name = "follow") 
date_follow = models.DateTimeField(auto_now=True, editable = False)
+2  A: 

Maybe something like (using Q objects):

blogs = New.objects.filter(Q(created_by = request.user) | Q(relations_set__initiated_by = request.user))

This assumes that you have a foreign key from Relations to New. See the documentation about following relationships backward.

If your Relations model doesn't have a foreign key to New but a foreign key to the users table, say following = models.ForeignKey(User), then you can maybe do something like

blogs = New.objects.filter(Q(created_by = request.user) | Q(created_by__follow__initiated_by = request.user))

Now, your foreign key from the Relations model to the user model should have related_name = 'follow'.

muksie
hmm... i can't use expressions in Q objects. it says: "keyword can't be an expression"
dana
My mistake, the dot in relations_set.initiated_by should have been a double underscore, since it is a keyword argument to Q. I have fixed it.
muksie
now it says: "Cannot resolve keyword 'relations_set' into field. Choices are: created_by, date, id, post, reply, vote" meaning the fields belonging to 'New' table. It doesn't let ne query on the fields of another table, though i am making a join... :(
dana
I edited my answer. You have to add a foreign key from Relations to New.
muksie
hmmm.... i see... but i don;t really understand the logical of adding a FK from Relations to New... it means every follow relation should have associated one "New" Blog post.though there there are many blog posts from a 'followed' user..hmm...
dana
I guess what you mean is you have a manytomany instead of the foregin key between them?
lazerscience
yes, that may be... but do i need a foreign key for the join ? (is it the only way?)
dana
well can you post the definition of the two models again? (or at least how you defined the relation between them)
lazerscience
I added a new case in the answer which should work if your Relations model doesn't have a foreign key to the blog table, but does have two foreign keys to users, initiated_by and following.
muksie
now it says: "global name 'relations_set__follow' is not defined" though i am using the correct namefields (i've edited the tables above).@lazerscience - edited and added the tables
dana
I updated my answer, now it should work for your data model.
muksie
yess!! it works now and i finally understood the Q objects. (it will surely help me further in my projects).Thanks so much!
dana