0

I want to have facebook kind of news feed, in which i need to fetch data from 2 different models ordered by time.

Models are something like :

class User_image(models.Model):
user = models.ForeignKey(User_info)
profile_pic = models.ImageField(upload_to='user_images')
created = models.DateTimeField(auto_now_add=True)

class User_status(models.Model):
user = models.ForeignKey(User_info)
status = models.CharField(max_length=1)
created = models.DateTimeField(auto_now_add=True)

As per my requirement, i can not make a single model out of these two models.

Now i need to know the simple code in views and template so as to display profile pic and status in the news feed according to time.

Thanks.

D Dhaliwal
  • 552
  • 5
  • 23
  • What the problem, what have you tried and what don't you understand? Have you looked up how to merge quersets? Have you looked up how sort lists by attribute? We're not here to write your program for you. – Timmy O'Mahony Jun 11 '13 at 19:33
  • 1
    @TimmyO'Mahony, well we are here to help others if we know something, so better you should focus on providing the solution or not replying at all. Thanks. – D Dhaliwal Jun 11 '13 at 20:30
  • I understand that and I was probably a bit harsh above so apologies, but asking for both view and template code without specifying what step you are having trouble with (merging/sorting) is a little bit lazy. There are [previous highly visible questions](http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view) that address these issues. – Timmy O'Mahony Jun 11 '13 at 21:17

2 Answers2

1

The most simple way of archiving this is to have a base model, call it Base_event,

class Base_event(models.Model):
     user = models.ForeignKey(User_info)
     created = models.DateTimeField(auto_now_add=True)

and derive both your models from this Base. This way you write less code, and you archive your objective. Notice that you have to make an implementation choice: how will they will inherit from base. I advice to read Django documentation to help you choose wisely according to what you want to do.

EDIT:

I would notice that the accepted answer has a caveat. It sorts the data on the python and not on the mysql, which means it will have an impact on the performance: the whole idea of mysql having SORT is to avoid having to hit the database and them perform the sorting. For instance, if you want to retrieve just the first 10 elements sorted, with the accepted solution you have to extract all the entries, and only then decide which ones are the first 10.

Something like Base_event.objects.filter().sort_by(...)[10] would only extract 10 elements of the database, instead of the whole filtered table.

The easy solution now becomes the problem later.

Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
  • 1
    actually mine problem is bigger one so i tried explaining in this short example, i got the solution as suggested by amarpreet to create a list chain and getting the query sets in a list. Thanks for your reply. – D Dhaliwal Jun 11 '13 at 20:36
  • I have got your point, actually earlier my concern was to fetch the data and now is to include efficiency. I have read somewhere that multi table inheritance is not much efficient. Is this true? – D Dhaliwal Jun 13 '13 at 17:20
0

Try something like creating list chain.

feed = list(chain(User_image,User_status))
feed = sorted(feed, key=operator.attrgetter('date_added'))

for those who refer it as not correct. https://stackoverflow.com/a/434755/2301434

Community
  • 1
  • 1
Amarpreet Singh
  • 2,242
  • 16
  • 27
  • This is not correct answer. Did you tried `django-activity-stream`? –  Jun 12 '13 at 08:44