# insert pandas dataframe from API into model without a form

**URL:** <https://forum.djangoproject.com/t/insert-pandas-dataframe-from-api-into-model-without-a-form/12531>\
**Category:** Forms & APIs\
**Created:** [March 10, 2022, 5:07am UTC](https://forum.djangoproject.com/t/insert-pandas-dataframe-from-api-into-model-without-a-form/12531 "2022-03-10T05:07:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwatt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dwatt/32/7723_2.png) [@dwatt](https://forum.djangoproject.com/u/dwatt)\
**Post date:** [March 10, 2022, 5:07am UTC](https://forum.djangoproject.com/t/insert-pandas-dataframe-from-api-into-model-without-a-form/12531/1 "2022-03-10T05:07:30Z")

</div>

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.

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

```

Cheers

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 10, 2022, 12:28pm UTC](https://forum.djangoproject.com/t/insert-pandas-dataframe-from-api-into-model-without-a-form/12531/2 "2022-03-10T12:28:38Z")

</div>

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](https://docs.djangoproject.com/en/4.0/topics/db/models/#module-django.db.models). 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.](https://docs.djangoproject.com/en/4.0/intro/tutorial02/#playing-with-the-api)

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](https://docs.djangoproject.com/en/4.0/topics/db/queries/#creating-objects)

---

<div class="post-metadata">

**Author:** ![dwatt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dwatt/32/7723_2.png) [@dwatt](https://forum.djangoproject.com/u/dwatt)\
**Post date:** [March 11, 2022, 12:21am UTC](https://forum.djangoproject.com/t/insert-pandas-dataframe-from-api-into-model-without-a-form/12531/3 "2022-03-11T00:21:12Z")

</div>

Hi Ken,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 11, 2022, 12:52am UTC](https://forum.djangoproject.com/t/insert-pandas-dataframe-from-api-into-model-without-a-form/12531/4 "2022-03-11T00:52:41Z")

</div>

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.
