insert pandas dataframe from API into model without a form

Hi,

I’m relearning Django and trying to work out the best way to insert data into a model without using a form.

I have a python script that is pulling crypto prices from an API, ideally i’d like this updating every 5 mins.

I can easily just use df.to_sql() but assuming the best practice would be using a model.

Can anyone advise on the best way to do this. Just to to reiterate:

  • No form is needed

  • The data is being called from an API every N minutes into a dataframe and will replace the data each time.

# Model
class TrendingCoins(models.Model):
    symbol = models.CharField(max_length=20)
    name = models.CharField(max_length=20)

Cheers

Are you creating new instances these models or are you updating existing instances?

The answers are (slightly) different depending upon which situation you’re addressing.

Start by reviewing the docs at Models | Django documentation | Django. There are numerous examples on that page of instances being created.

Also, if you’ve worked your way through the official Django tutorial, you “manually” created objects in part 2.

If you’re updating existing objects, the process is a little different. You (generally) query for the object to be updated, change the individual fields with the new values, and then save the updated instance.

Also see Making queries | Django documentation | Django

Hi Ken,

I’ve applied to the following code with some success:

class CoinManager(models.Manager):
    def create_coin(self, symbol, name):
        coin = self.create(symbol=symbol, name=name)
        return coin

class TrendingCoins(models.Model):
    symbol = models.CharField(max_length=20)
    name = models.CharField(max_length=20)

    objects = CoinManager()

# tr is a dataframe created from API call
for idx, row in tr.iterrows():
    TrendingCoins.objects.create_coin(symbol=row['symbol'], name=row['name'])

Every time i run python manage.py migrate it appends data. As I’m looking to replace the data, I guess deleting all values would be best.

Three follow up questions from me (I’ve read the docs you provided and couldn’t find the answer):

  1. How to replace the data?

  2. How can I get this to update every N minutes?

  3. Is using python manage.py migrate to update the tables best practice?

Cheers

How, what, or why are you using migrate as any part of this process?

What did you create as a migration file?

You mentioned that you’re relearning Django. You should probably work your way through either the official Django Tutorial or the Django Girls tutorial. There’s some fundamental Django knowledge that you appear to be missing.

The easiest way to run a script periodically is by using a custom admin command which you schedule using something like cron.