django模型不会同意夹具

时间:2015-01-28 04:32:43

标签: python django postgresql fixtures

我正在尝试使用django中的fixture来使用初始值填充postgresql数据库。即使我的模型看起来像这样,我仍然会收到这些奇怪的Could not load publication.Article(pk=None): value too long for type character varying(100) 错误:

class Article(models.Model):
    _id = models.CharField(max_length=1000)
    author_name = models.CharField(max_length=1000)
    caption = models.CharField(max_length=1000)
    isGraphic = models.BooleanField(max_length=1000, default=True)
    pictures = models.URLField(max_length=1000)
    text = models.CharField(max_length=10000)
    title = models.CharField(max_length=1000)
    user_img = models.URLField(max_length=1000)
    videoname = models.CharField(max_length=1000)
    vimeo_id = models.IntegerField(max_length=1000)

Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute output = self.handle(*args, **options) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 61, in handle self.loaddata(fixture_labels) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 91, in loaddata self.load_label(fixture_label) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 148, in load_label obj.save(using=self.using) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save models.Model.save_base(self.object, using=using, raw=True) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/base.py", line 698, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/base.py", line 731, in _do_insert using=using, raw=raw) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/query.py", line 921, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 920, in execute_sql cursor.execute(sql, params) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/Users/sam.royston/PycharmProjects/sahelien_d/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) django.db.utils.DataError: Problem installing fixture '/Users/sam.royston/PycharmProjects/sahelien_d/sahelien_django/fixtures/test.json' : Could not load publication.Article(pk=None): value too long for type character varying(100)

为什么我会收到此错误?

test.json:

[
    { "model" : "publication.Article" , "fields": 
         { 
           "_id" : "5306dfa9ed2379f03a000001" , 
           "author_name" : "Sahélien Tombouctou", 
           "caption" : "Les  n’ont fait aucune victime, ni de dégâts  matériels",  
           "isGraphic" : false,  
           "pictures" : [], 
           "text" : "La ville de Tombouctou a reçu des tirs d'obus dans la nuit de dimanche. \n<br>\n<br>\nLes deux premiers obus sont tombés dans la localité de Kabara, à 10km de la cité des 333 saints. Le troisième obus est tombé sur la route de Goundam.\n<br>\n<br>\nLes tirs n’ont fait aucune victime, ni de dégâts matériels. Selon le lieutenant-colonel Seydou Koné, en poste à Tombouctou, l'armée malienne est mobilisée pour déterminer l'origine de cette attaque.",                       
           "title" : "Tombouctou attaquée à la roquette", 
           "videoname" : "okok.mp4", 
            "vimeo_id" : "87246621"
         } 
    }
]

1 个答案:

答案 0 :(得分:0)

您的json夹具缺少主键。 Django automatically adds a primary key给你的模特;叫id。由于此键是必需的,您应该在灯具中提供它。

您发布的灯具没有此键,您应该添加它:

[
    { "model" : "publication.Article" , "fields": 
         { 
           "id": "1",
           "_id" : "5306dfa9ed2379f03a000001" , 
           "author_name" : "Sahélien Tombouctou", 
           "caption" : "Les  n’ont fait aucune victime, ni de dégâts  matériels",  
           "isGraphic" : false,  
           "pictures" : [], 
           "text" : "La ville de Tombouctou a reçu des tirs d'obus dans la nuit de dimanche. \n<br>\n<br>\nLes deux premiers obus sont tombés dans la localité de Kabara, à 10km de la cité des 333 saints. Le troisième obus est tombé sur la route de Goundam.\n<br>\n<br>\nLes tirs n’ont fait aucune victime, ni de dégâts matériels. Selon le lieutenant-colonel Seydou Koné, en poste à Tombouctou, l'armée malienne est mobilisée pour déterminer l'origine de cette attaque.",                       
           "title" : "Tombouctou attaquée à la roquette", 
           "videoname" : "okok.mp4", 
            "vimeo_id" : "87246621"
         } 
    }
]

您缺少夹具中模型所需的关键字段。您需要添加user_img,而pictures不能为空。

灯具需要传递模型的所有验证规则;并且因为根据您的模型需要所有字段,所以它们都需要在夹具中可用。

此外,对于不适用的整数,布尔和url字段,您有一个max_length参数。