Red Devil
Red Devil

Reputation: 19

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..

Upvotes: 0

Views: 2709

Answers (3)

Semisi Taufa
Semisi Taufa

Reputation: 19

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.

Upvotes: 0

Ski
Ski

Reputation: 14487

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, otherwise dumpdata will include duplicate objects.
  • loaddata for SubsetOfModelX will fail if there are any null=False fields on ModelX that are not on the subset model.

Upvotes: 1

Rob Osborne
Rob Osborne

Reputation: 4997

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.

Upvotes: 1

Related Questions