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.
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):
How to replace the data?
How can I get this to update every N minutes?
Is using python manage.py migrate to update the tables best practice?
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.