Spinnaay
Spinnaay

Reputation: 129

Replace database value when recalling in Django

I'm trying to change the value of database entries when I recall them in Django.

So I have a database with lots of ON and OFF values as 'value' and time as 'time'.

When I recall these items to display and graph them I want to be able to change all of the 'ON' to 1 and the 'OFF' to 0.

I'm doing a basic recall like this

items = Item1.objects.all()

Then I want to loop over them and make the changes. Something like.

for a in items:
   if items.value == 'ON':
     items.value[a] = 1
   elif items.value == 'OFF':
     items.value[a] = 0

Forgive me, I'm still not sure what form items takes on once you use the .object function on it. Is it a dictionary?

Thanks!

Upvotes: 0

Views: 470

Answers (2)

Radek
Radek

Reputation: 1630

I understand you want to transform values on the fly (On => 1, Off => 0).

As you want to transform the values for display purposes only, you have several options to deal with that (none will change the data in DB):

1a. transform values in the custom SQL query using Custom Manager in models.py - https://docs.djangoproject.com/en/1.9/topics/db/managers/#adding-extra-manager-methods

1b. transform values in the custom raw SQL in your views - https://docs.djangoproject.com/en/1.9/topics/db/sql/#adding-annotations

  1. add derived attribute in your model class using Model Method in models.py

  2. transform values in the view.py code generating the proper values for context in templates

  3. transform values in the template code

Where you do that depends on whether and where you want to use the original on/off values and where you want to put the transformation workload.

If you do not need the original values to be processed in Django, then change them on the database level (in the read query), so Django already receives the 0/1 values.

However you might find it easiest to follow option 2 (in models.py).

class Item1(models.Model):
    ... your current definitions

    def _bin_value(self):
        "Returns the tranformed ON/OFF value to 0/1."
        if self.value == "ON":
             return 1 
        if self.value == "OFF":
             return 0 
        return None # you should define how you handle empty, NULL or unexpected value
    bin_value = property(_bin_value)

And use bin_value in your views instead of value.

Upvotes: 1

Dorian Dore
Dorian Dore

Reputation: 962

Item1.objects.all() returns a queryset (very similiar to a list) containing all the Item objects. The actual Item1 class just takes the place of a table in a database. Doing Item1.objects.all()[0] will give you the first item object in the queryset. You can also use the .order_by method on a queryset to order it by a specific value, such as date or a vote value. Also, after you operate on a database object, you need to save it. So say you have this code:

items = Item1.objects.all()

Then you'd do

for item in items:
    if item.value == 'OFF':
        item.other_value == 1
    item.save()

Upvotes: 0

Related Questions