JSON Models Django


JSON Models Django



I have the following models:


class Estado(models.Model):
nome = models.CharField('Estado', max_length=30)
uf = models.CharField('UF', max_length=2)

class Cidade(models.Model):
municipio= models.CharField('Municipio', max_length=50)
estado = models.ForeignKey(Estado, on_delete=models.DO_NOTHING)



How can i create a .json fixture for them?



To add the states, I did the following:


[{
"model": "comum.estado",
"pk": 1,
"fields": {
"uf": "AC",
"nome": "Acre"
}
},
...etc...
]



To add cities, I tried doing the following, but it did not work:


[{
"model": "comum.cidade",
"pk": 1,
"fields": {
"municipio": "Afonso Cláudio",
"estado": 8
}
},
...etc...
]



What should I do to fix it?




1 Answer
1



in cities model field estado is foreign key. so you must first add related object then create it.



simply add it to fixture before this item






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?