简体   繁体   中英

Django: Fetch most recently inserted SPECIFIC field value in database error:need more than 1 value to unpack

I have a database consisting multiple fields, but I just want to get the value of one field ("name") which is most recently inserted into the database.

There are some codes that I tried in views.py, but they all return errors:

name=Record.objects.values_list('name', flat=True).latest('id')
name=Record.objects.values_list('name', flat=True).latest('name')
name=Record.objects.values_list('name', flat=True).order_by('-id')[:1]
name=Record.objects.values('name').latest('id')

erorrs: need more than 1 value to unpack

How can I resolve this issue? thanks.

The views.py code for your reference.

class XXXView(ListView):
    context_object_name = XXX'
    template_name = 'XXX.html'

    queryset = xxx.objects.all()
    name=entry.objects.latest('id').name

    if name is not None:
        queryset=queryset.filter(name=name)

    sales=xxx.objects.filter(queryset).aggregate(Sum('sales'))

    def get_context_data(self, **kwargs):
        context = super(XXXView, self).get_context_data(**kwargs)
        context['bbb'] = entry.objects.order_by('-id')[:1]
        return context

You should ask for the name after the latest() , ie:

name=Record.objects.latest('id').name

values('name') throws away everything except the names, so after that call you can't find the last id any longer.

Try this:

name = Record.objects.values_list('name', flat=True).last()

latest(field_name) returns the latest object in the table, by date, using the field_name provided as the date field.

The latest and order_by methods needs to be run on a Queryset . The values_list method returns a list so that is why you get an error.

In addition, latest returns an object, not a Queryset so switching the order of the two methods will not be enough since values_list also has to be run on a Queryset

So, either just use the object:

name=Record.objects.latest('id').name

or more efficiently:

name=Record.objects.only('name').latest('id').name

Just as a note. Using the latest function in this way might be misleading. Using the latest with id as a parameter will give you the object with the largest id in the database. Depending on what you want it might be better to add a DateTimeField with an auto_now_add/auto_now instead and use that as the basis for the latest query. This will make it more clear what "latest" means in your specific case.

If find the QuerySet documentation to be really good: https://docs.djangoproject.com/en/1.8/ref/models/querysets/

And if you want to test things out to find the problem yourself next time I can really recommend running python manage.py shell and importing your model to test your one liners. You should most likely look into tests as well which can give you a better interface for troubleshooting once things get more complicated: https://docs.djangoproject.com/en/1.8/topics/testing/overview/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM