Reputation: 103
I have table in the DB say MyHistory table. Based on event history , I have to pick latest event in my events say LOGIN and SUPPORT. I have to pick whatever the event performed latest.
Note : Event does not have priority that particulate event has to perform order.
While running the below code , I got the error like
ERROR: error - too many values to unpack
What is the wrong in the below code?
def my_welcome_msg(self):
msg_created_date = MyHistory.objects.select_related().filter(
cluster=self.cluster).filter(
event_type=MyHistory.SUPPORT).filter(MyHistory.LOGIN).order_by('created_date')[0].created_date
return msg_created_date
I have records related to single user_id in a table as shown below.
| id |created_date|modified_date| description |user_id | event_type | cluster_id
1 2017-05-31 01:00:58 SUPPORT
2 2017-05-30 23:00:38 LOGIN
Upvotes: 0
Views: 51
Reputation: 15846
There're several problems with your code. Firstly, Django doesn't support empty select_related
for a long time, you're either using a very outdated Django version or should explicitly list all related models to fetch. Secondly, your code tries to fetch MyHistory
objects that has mutually exclusive types. Thirdly, your code is ordering by date in the wrong order.
last_msg = (MyHistory.objects
.filter(cluster=self.cluster)
.filter(event_type__in=[MyHistory.SUPPORT, MyHistory.LOGIN])
.order_by('-created_date').first())
return last_msg.created_date if last_msg else None
Upvotes: 1