【发布时间】:2017-06-20 13:54:36
【问题描述】:
我刚刚安装了this django-csvimport package。现在我想覆盖管理区域表单中的默认值。我找到了code here,它定义了模型,并包含了当前的默认文本:
class CSVImport(models.Model):
""" Logging model for importing files """
model_choice = []
model_name = models.CharField(max_length=255, blank=False,
default='csvimport.Item',
help_text='Please specify the app_label.model_name',
choices=get_models())
field_list = models.TextField(blank=True,
help_text='''Enter list of fields in order only if
you dont have a header row with matching field names, eg.
"column1=shared_code,column2=org(Organisation|name)"''')
upload_file = models.FileField(upload_to='csv', storage=fs)
file_name = models.CharField(max_length=255, blank=True)
encoding = models.CharField(max_length=32, blank=True)
upload_method = models.CharField(blank=False, max_length=50,
default='manual', choices=CHOICES)
error_log = models.TextField(help_text='Each line is an import error')
import_date = models.DateField(auto_now=True)
import_user = models.CharField(max_length=255, default='anonymous',
help_text='User id as text', blank=True)
def error_log_html(self):
return re.sub('\n', '<br/>', self.error_log)
error_log_html.allow_tags = True
def __unicode__(self):
return self.upload_file.name
例如,我想用其他东西覆盖model_name 字段默认csvimport.Item。我有点茫然如何覆盖它,因为我没有用于 csvimport 的应用程序文件夹,因为它是第三部分安装。这将是我第一次覆盖第三方安装的应用程序模型。
现在我对其进行了更多研究,不确定是否应该覆盖此模型,或者可能更好地覆盖 admin.py 文件的 ModelAdmin 类?
谢谢!
【问题讨论】:
-
我认为最好的方法是在您的 django 项目中创建一个新应用程序。在那里,您可以在其 models.py 中导入上述 CSVImport 类,然后您可以对其进行子类化和使用。在这种情况下,您可以保持 django-csvimport 包不变。
-
子类化在这里不起作用我看到了代码,它只知道导入自己的模型而不是其他任何模型。
标签: python django model overriding