views:

104

answers:

2

Hi.

I'm new to python and django.

Apps | Versions:

  • Python 2.6.2
  • Django (working with PostgreSQL)

Question: I wrote a simple model:

class OperationType(models.Model):
    eid            = models.IntegerField()
    name           = models.TextField(blank=True)
    description    = models.TextField(blank=True)

    def __unicode__(self):
        tpl = 'eid="', str(self.eid), '" name="', self.name, '"'
        return ''.join(tpl)

Now I need to initialize it, for example with this data:

0, "None"
1, "Add"
2, "Edit"
3, "Delete"

But I need to initialize this data not with admin web panel, but after class model created in the same code. How to do this?

Thanks for help!


ADDED: file initial_data.json:

[
  {
    "model": "OperationType",
    "pk": 1,
    "fields": {
      "eid": 0,
      "name": "None",
      "description": "Do nothing"
    }
  },
  {
    "model": "OperationType",
    "pk": 2,
    "fields": {
      "eid": 1,
      "name": "Add",
      "description": "Adding transaction"
    }
  }
]
+2  A: 

Here.

Ignacio Vazquez-Abrams
@Ignacio Vazquez-Abrams: Thanks for reply. I tried to use initial_data.json file, with content (added to the question), but when I run *python manage.py syncdb*, It return me the error: *DeserializationError: Invalid model identifier: 'OperationType'*? What i'm doing wrong?
mosg
@Ignacio Vazquez-Abrams: Done! :) i was need *accounting.operationtype*!
mosg
+1  A: 

You can use fixtures, check the Django Document.

Satoru.Logic
@Satoru.Logic +1 Thanks.
mosg