2015-09-25 69 views
1

加載初始數據,我試圖讓熟悉加載初始數據與loaddata的典範。所以,我的JSON看起來像這樣:「詮釋」對象有沒有屬性「取代」在Django

[ 
    { 
    "model": "locations.location", 
    "pk": 1, 
    "fields": { 
     "name": "Cafe" 
    } 
    } 
] 

我model.py:

class Location(models.Model): 
    id = models.UUIDField(primary_key=True, default=uuid.uuid4) 
    create_date = models.DateTimeField('date added', auto_now_add=True) 
    modify_date = models.DateTimeField('date modified', default=timezone.now) 
    name = models.CharField(max_length=200) 

,然後當我做python manage.py loaddata mydata我得到

django.core.serializers.base.DeserializationError: Problem installing fixture '/path/to/locations/fixtures/mydata.json': 'int' object has no attribute 'replace'

這是什麼意思?我也試過在models.py

def __unicode__(self): 
    return self.name 

改變這

def __unicode__(self): 
    return self.name.get_full_name() 

,但沒有奏效。這個int對象是什麼?

更多回溯:

File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 390, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 441, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 60, in handle 
    self.loaddata(fixture_labels) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 90, in loaddata 
    self.load_label(fixture_label) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 141, in load_label 
    for obj in objects: 
    File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/json.py", line 84, in Deserializer 
    six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2]) 
    File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/json.py", line 78, in Deserializer 
    for obj in PythonDeserializer(objects, **options): 
    File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/python.py", line 104, in Deserializer 
    data[Model._meta.pk.attname] = Model._meta.pk.to_python(d.get("pk", None)) 
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 2404, in to_python 
    return uuid.UUID(value) 
    File "/usr/lib/python2.7/uuid.py", line 131, in __init__ 
    hex = hex.replace('urn:', '').replace('uuid:', '') 
+0

那是你的所有mydata.json文件? – Gocht

+0

是的,我想嘗試一下,如果它工作,所以我想一個字段添加到我的模型 – Tom

+0

顯示模型,請。 – Gocht

回答

0

此作品感謝所有評論:

[ 
    { 
    "model": "locations.location", 
    "fields": { 
     "id":"a3184190-0b97-4c56-8aac-eeee8875aaa2", 
     "name": "Cafe" 
    } 
    } 
] 
相關問題