--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/88f11149-eb65-4d0b-ad6e-729d9db6c6d2%40googlegroups.com.
--
Thanks for the recommendation. The raw SQL method worked for me. Would be good to know how to accomplish this using the Django ORM query set.
Basically I have a list of games, the user may choose to update the name for example and instead of the object being updated a new object is created with the status set to PENDING.
The application should only display a single unique game (game_id) where the latest(created_at) record status is (APPROVED).
Example, I have cut out most of the bulk one to many links, fields etc. so it is easier to share:
My View:
class ViewGameList(ListView):
model = GameDetails
template_name = 'games/list_all_games.html'
paginate_by = 60 # if pagination is desired
def get(self, request, **kwargs):
self.object_list = GameDetails.objects.raw("""
SELECT temp.*
FROM gamedetails temp
INNER JOIN
(SELECT game_id, MAX(created_at) AS MaxDateTime, MAX(id) AS MaxID
FROM gamedetails
WHERE status = ‘APRO’
GROUP BY game_id) grouped_join
ON temp.game_id = grouped_join. game_id
AND temp.created_at = grouped_join.MaxDateTime
AND temp.id = grouped_join.MaxID
""")
return self.render_to_response(self.get_context_data())
My Model:
class GameDetails(models.Model):
name = models.TextField(blank=False)
game = models.ForeignKey(Game, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
APPROVED = 'APRO'
PENDING = 'PEND'
DECLINED = 'DECL'
STATUS_TYPES = (
(APPROVED, 'Approved'),
(PENDING, 'Pending Approval'),
(DECLINED, 'Declined'),
)
status = models.CharField(max_length=25, choices=STATUS_TYPES, default=PENDING)
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABexzmgXY0XHjsLNe2%2BTwN1F8cJwX9%3DHYL2EoF56XzzD4M_xew%40mail.gmail.com.