views:

55

answers:

2

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 like 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....?

+1  A: 

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.

Ignacio Vazquez-Abrams
A: 

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.

rebus