My models.py includes several fields. The primary key is by default, set automatically by Django.
I have a Django crontab that attempts to update each entry on a regular basis, but can no longer save. (Until recently, I had manually set the primary key based on one of these fields.)
Could someone suggest how to get around this. For each entry, I’d like to be able to update each of the declared fields and create if non-existent.
I was under the impression that save does both create or update as neccessary
class ABC(models.Model):
init = models.CharField(max_length=6)
last = models.CharField(max_length=20)
fullid = models.CharField(max_length=30) <--- used to be primary_key=True
Crontab (pseudocode)
for x in list:
try:
entry = init='abc', last='def', fullid='xyz'
entry.save()
except: 'unable to update.' <-- I'm now hitting the except all the time.
Use something like that:
Never ever use
try: ... except: ...because this way you don’t even know what have failed inside. Catch only “awaited” exceptions which you do know how to process. Or, if you want it to be bulletproof and unstoppable – at least display catched exception info.