0

I have two models:

class ModelOne(models.Model):
   name = models.TextField()
   year = models.IntegerField()

class ModelTwo(models.Model):
   name = models.TextField()
   year = models.IntegerField()

and respectively their Entry Models

class EntryCenter(models.Model)
   name = models.TextField()
   user = models.ForeignKey(User,related_name="user_entrycenters") 

class EntryOne(models.Model)
   entrycenter = models.ForeignKey(EntryCenter,related_name="center_one_entries")
   model_one = models.ForeignKey(ModelOne,related_name="model_one_entries")
   added = models.DateTimeField(auto_now_add=True)
   user = models.ForeignKey(User,related_name="user_one_entries") 

class EntryTwo(models.Model)
   entrycenter = models.ForeignKey(EntryCenter,related_name="center_two_entries")
   model_two = models.ForeignKey(ModelTwo,related_name="model_two_entries") 
   added = models.DateTimeField(auto_now_add=True)
   user = models.ForeignKey(User,related_name="user_two_entries")

and now i am in EntryCenter XY, i need to query both entries in this Center and sort them by year with oldest year first and render to template.

center = EntryCenter.objects.get(id=1)#<-- EntryCenter XY
one = center.center_one_entries.order_by('model_one__year')
two = center.center_two_entries.order_by('model_two__year')
entries = sorted(chain(one, two), key=lambda obj: obj.year)

one is for example [1, 3, 4] two is for example [2, 5, 6]

i need [1,2,3,5,4,6], how can i achieve this?

doniyor
  • 36,596
  • 57
  • 175
  • 260
  • 1
    Does `key=lambda obj: getattr(obj, 'model_one', getattr(obj, 'model_two')).year` work? – alecxe Mar 11 '14 at 03:56
  • @alecxe, thanks dude, but i changed my question.. first one was my typo. but your solution might help me with the second question as well.. let me check – doniyor Mar 11 '14 at 03:59
  • Well, speaking about the first question..take a look: http://stackoverflow.com/questions/431628/how-to-combine-2-or-more-querysets-in-a-django-view – alecxe Mar 11 '14 at 04:01

0 Answers0