Exclude multiple fields from datadump in django
Is there a way to exclude fields from different tables while taking DB dump. I could only find to exclude a model completely. (I am using postgresql). any help is appreciated.. Thanks in advance开发者_运维百科..
Not easily. I would grab a copy of dumpdata.py from the Django source and put it into your project and customize it. It would not be too hard to extend it to either use a custom manager for dumping or extend the exclude to support app.model.field.
That sounds like a generally useful extension.
One possible trick is to define a new model with a subset of fields and set managed=False
and override db_table
fields in Meta
.
It is also possible to avoid repetition defining common fields through common abstract parent model.
class CommonBase(models.Model):
class Meta:
abstract = True
class ModelX(CommonBase):
pass
class SubsmetOfModelX(CommonBase):
class Meta:
managed = False
db_table = 'app_label_modelx'
Drawbacks of this approach might be:
- Always need to exclude at least one model of two above during
dumpdata
, otherwisedumpdata
will include duplicate objects. loaddata
forSubsetOfModelX
will fail if there are anynull=False
fields onModelX
that are not on the subset model.
You can dump it into a JSON file and then use Notepad++ or equivalent to remove every line that has your field_name. Works for me, and very quick too. Make sure your field name is unique so that it will not remove other rows.
精彩评论