2016-05-31 27 views
0

關於django導入導出的一個簡短問題。假設我有喜歡的人在docs的典範,但也有一些額外的限制(注意是元類):django導入導出中的異常處理

class Book(models.Model): 
     name = models.CharField('Book name', max_length=100) 
     author = models.ForeignKey(Author, blank=True, null=True) 
     author_email = models.EmailField('Author email', max_length=75, blank=True) 
     imported = models.BooleanField(default=False) 
     published = models.DateField('Published', blank=True, null=True) 
     price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True) 
     categories = models.ManyToManyField(Category, blank=True) 

     def __unicode__(self): 
      return self.name 

     class Meta: 
      unique_together = ('name', 'author') 

在批量上傳,我就非常喜歡,包含錯誤(重複的條目在這種情況下,任何行 - 但也可能是其他類型的「損壞」行)被跳過,其餘的上傳繼續。應該將損壞的行記錄到包含有問題的行和帶有異常名稱的附加列的文件中。

有一個通用exceptions.py文件:

class ImportExportError(Exception): 
    """A generic exception for all others to extend.""" 
    pass 


class FieldError(ImportExportError): 
    """Raised when a field encounters an error.""" 
    pass 

但目前還不清楚如何處理與行的情況和跳躍行。來自任何處理此事的人的任何幫助將不勝感激。

回答

0

documentation是相當清楚的:

dry_run是決定是否對數據庫的更改是 作出或如果導入只是模擬一個布爾值。它默認爲False。

raise_errors是一個布爾值。如果爲True,則導入應該引發錯誤。 默認值爲False,這意味着最終錯誤和回溯將在Result實例中保存爲 。