Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django get_or_create with defaults parameter

I have a syntax error with the following code:

ids_row={}
ids_row["releve_annee"]=int(row[0])
ids_row["releve_mois"]=int(row[1])
ids_row["no_ordre"]=int(row[2])
defaults={}
defaults["titre_rmc"]=row[3].strip()
defaults["notes"]=row[17].strip()
instance, created = Act.objects.get_or_create(**ids_row, defaults=defaults)

My model:

class Act(models.Model):
    releve_annee=models.IntegerField(max_length=4, blank=False, null=False)
    releve_mois=models.IntegerField(max_length=2, blank=False, null=False)
    no_ordre=models.IntegerField(max_length=2, blank=False, null=False)
    titre_rmc=models.CharField(max_length=2000, blank=False, null=False)
    notes=models.CharField(max_length=2000,  blank=True, null=True, default=None)
        class Meta:
        unique_together=(("releve_annee", "releve_mois", "no_ordre"), )

In a shell:

instance, created = Act.objects.get_or_create(**ids_row, defaults=defaults)
                                                       ^
SyntaxError: invalid syntax

If I remove defaults=defaults, the error disappears. What is wrong?

like image 563
rom Avatar asked Nov 11 '13 19:11

rom


1 Answers

Try changing the sequence of parameters,

instance, created = Act.objects.get_or_create(defaults=defaults, **ids_row)
like image 130
Rohan Avatar answered Oct 26 '22 07:10

Rohan