Monday, May 23, 2016

Re: get all columns as a list



On Mon, May 23, 2016 at 9:20 AM, Larry Martell <larry.martell@gmail.com> wrote:
They're not identical - there's a timestamp - that is not one of the
columns compared.

The data is status data from a piece of equipment and we only want to
store changes. If 2 consecutive rows come in that are the same
(excluding the timestamp) I don't want to store the second one.


If you can coerce your incoming data into a dict using the same structure as your model, you can probably do something like this:

new_data = {'col1': data1, 'col2': data2}
latest_db_record = FOO.objects.filter(bar='baz').order_by('-timestamp').values('col1', 'col2')[0]

if new_data != latest_db_record:
    new_data['bar'] = 'baz'
    FOO.objects.create(**new_data)

Salt to taste as necessary.

You might also be able to work with qs.last() or qs.latest(), but those return the actual objects and you can't take advantage of qs.values() splitting it into a dict for you.

A DB transaction may also be appropriate if you have a lot of data rapidly coming in. 

-James

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWkVCzxVCOUJrPFaPM8yEQZa1oJ_3OGZ5U2Y%2BhSRJt2LQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment