English 中文(简体)
带有默认(自动)主密钥的Django 模型 - 自动更新 vs 保存条目
原标题:Django models with default (automatic) primary key - upate vs save entries

我的模型. py 包含多个字段。 主键默认为默认键, 由 Django 自动设定 。

我有一个Django Crontab, 试图定期更新每个条目, 但无法再保存。 (直到最近, 我手动根据其中一个字段设置了主键 。 )

是否有人可以建议如何绕过它。 对于每个条目, 我希望能够更新每个已宣布的字段, 并创建不存在的域 。

" 强 " 我当时的印象是,除去会创造或更新为 " /强 " 。

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.
最佳回答

使用类似的东西 :

try: 
    abc = ABC.objects.get(fullid=id) 
except ABC.DoesNotExist: 
    ABC.objects.create(params)
else:
    abc.field = value
    abc.save()

永远不要使用 < code> try:... 例外 :... , 因为这样你甚至不知道里面有什么失败了 。 只能捕捉“ 期待” 的例外, 您确实知道如何处理。 或者, 如果您想要它防弹和不可阻拦 - 至少显示被捕获的例外信息 。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签