开发者

Django. Update data in db. Easy question

开发者 https://www.devze.com 2023-01-06 21:39 出处:网络
Ok! I have this model: class my(models.Model): name = models.TextField() description = models.TextField()

Ok! I have this model:

class my(models.Model):
    name = models.TextField()
    description = models.TextField()
    created = models.DateTimeField()
    def __unicode__(self):
       return self.name

I'm gettin data to this model from mssql database and saving it. Something like this.

mysql_name='somedata'           #this data come from some connection with mssql
mssql_description='somedata'    #this data come from some connection with mssql
mssql_created='somedata'        #this data come from some connection with mssql

i'm creating field in my MySQL database and saving

mymodel=my(name=mysql_name, description=mssql_description, created=mssql_created)

mymodel.save()

Now it is in my database. I'm doing this in loop so it fill my database.

Then i need check the data in remote MSSQL server by using timestamp. I made SQL request So i'm getting only updated data. End I have to overwrite old data.

Question is how to overvrite old data? I'm little bit confuse.

could it be l开发者_运维问答ike this?

   mymodel=my.objects.get(name=mysql_Name)
   mymodel.description=new_updated_description
   mymodel.save

but if i do not have this field at all?....

I have to chek if i have it in my databse? How....?


As long as the same PK is used, the row will be overwritten. It is easiest to get() the existing row, modify the fields, and save() it.


You can use get_or_create on your models manager as a shortcut to create new instance of the model or to update the existing one. See docs for examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号