1

Purpose/Intension

I have created Django Model with the result being an sqlite3 database. I now want to use a separate python script to add to that database based on some conditional logic.

Problem

I am new to Django and Python so I am not sure how to interrogate the database and then add some values based on the return result from that interrogation using a separate python script.

This is my Models.py:

from django.db import models
from django.utils import timezone
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

# Create your models here.
class Author(models.Model):
    '''
    Model representing the Author of the stocknote.
    '''
    user_id = models.ForeignKey('auth.User')
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Stock(models.Model):
    '''
    Model representing the stock info.
    '''
    author = models.ForeignKey(Author)
    ticker_code = models.CharField(max_length=10, null=True, blank=True)
    latest_stock_price = models.DecimalField(max_digits=20, decimal_places=4, null=True, blank=True)
    datetime_of_last_quote = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.ticker_code

class Note(models.Model):
    '''
    Model representing the Authors note.
    '''
    author = models.ForeignKey(Author)
    note = models.TextField()
    ticker_code = models.ForeignKey(Stock)
    date_note_created = models.DateTimeField(default=timezone.now)
    alert_price = models.DecimalField(max_digits=20, decimal_places=4, null=True, blank=True)

    def __str__(self):
        return self.note

Now imagine I have added data into the database so it contains data in all the fields except for the field 'latest_stock_price' which is empty.

What i want to do in a separate python script - lets call it interrogate-db.py - is have programatic logic like so:

If 'GOOG' is in 'ticker_code' then
   add '1.234' to the latest_stock_price for GOOJ

The best i could achieve in writing interrogate-db.py is:

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject_project.settings')
import django
django.setup()
from myproject.models import Author, Stock, Note

def add_latest_price():
    for t in Stock.objects.values_list('ticker_code'):
        if 'GOOJ' in t:
            ...What goes here?...

What should be the syntax to add 1.234 to latest_stock_price if GOOJ is in t?

1 Answer 1

1
for stock in Stock.objects.all():
    if stock.ticker_code == 'GOOJ':
        stock.latest_stock_price = 1.234
        stock.save()

Or

Stock.objects.filter(ticker_code='GOOJ').update(latest_stock_price=1.234)
Sign up to request clarification or add additional context in comments.

3 Comments

It should be = in place of in and = instead of +=.
thanks @AshishNitinPatil I took that condition like it was python code. :P
Excellent! Thanks for the help Ravi and Ashish, that solved my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.