Django 1.6: How to ignore a fixture in python manage.py loaddata?

时间:2016-02-12 21:46:54

标签: python django django-1.6 django-fixtures loaddata

I need an answer for this, right now im doing this command:

public class AppsActivity extends AppCompatListActivity {
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private AppsAdapter listadaptor = null;
public static List<PackagesItem> Packages;
private boolean IsLightThemeEnabled;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SharedPreferences Prefs = PreferenceManager.getDefaultSharedPreferences(this);
    IsLightThemeEnabled = Prefs.getBoolean("Theme",false);
    if(IsLightThemeEnabled)
    {
        setTheme(R.style.AppTheme_Light);

    }
    else {
        setTheme(R.style.AppTheme);
    }

    if(getActionBar() !=null)
    {
       // getActionBar().set
    }

    setContentView(R.layout.appslist);

    packageManager = getPackageManager();

    new LoadApplications().execute();
}


@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);
    try {

        String AppPackage = app.packageName;
        String AppName = app.loadLabel(packageManager).toString();

        Packages = SharedPreferencesTools.getOrderedItems(this);

        boolean IsItAdded = false;
        for(PackagesItem list : Packages)
        {
            if(list.getPackage().equals(AppPackage))
            {
                Toast.makeText(this,AppName + " "+ getResources().getString(R.string.already_added_warning),Toast.LENGTH_LONG).show();
                IsItAdded = true;
                break;
            }
        }

        if(!IsItAdded) {
            Toast.makeText(this,AppName + " " + getResources().getString(R.string.added_success),Toast.LENGTH_LONG).show();
            Packages.add(new PackagesItem(AppPackage,""));
        }
        SharedPreferencesTools.setOrderedItems(this, Packages);

    } catch (ActivityNotFoundException e) {
        Toast.makeText(AppsActivity.this, e.getMessage(),
                Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(AppsActivity.this, e.getMessage(),
                Toast.LENGTH_LONG).show();
    }
    finish();
}

private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
    ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
    for (ApplicationInfo info : list) {
        try {
            if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
                applist.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return applist;
}

private class LoadApplications extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progress = null;

    @Override
    protected Void doInBackground(Void... params) {
        applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));

        listadaptor = new AppsAdapter(AppsActivity.this,
                R.layout.appitem, applist);

        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(Void result) {
        setListAdapter(listadaptor);
        progress.dismiss();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        progress = ProgressDialog.show(AppsActivity.this, null,
                "Loading application info...");
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}
}

This works fine, but now i want to do this same command but ignoring or skiping a simple file inside app/myapp/fixtures/ so i don't want to write one loaddata for every fixture inside, i wanted to make only one command and use something like --exclude or --ignore or some kind of way to do it in one line and keep the /* to go agains all the files inside.

Thanks in advance!

1 个答案:

答案 0 :(得分:2)

your own management command in Django很简单;继承自Django的loaddata命令使其变得微不足道:

<强> excluding_loaddata.py

from optparse import make_option

from django.core.management.commands.loaddata import Command as LoadDataCommand


class Command(LoadDataCommand):
    option_list = LoadDataCommand.option_list + (
        make_option('-e', '--exclude', action='append',
                    help='Exclude given fixture/s from being loaded'),
    )

    def handle(self, *fixture_labels, **options):
        self.exclude = options.get('exclude')
        return super(Command, self).handle(*fixture_labels, **options)

    def find_fixtures(self, *args, **kwargs):
        updated_fixtures = []
        fixture_files = super(Command, self).find_fixtures(*args, **kwargs)
        for fixture_file in fixture_files:
            file, directory, name = fixture_file

            # exclude a matched file path, directory or name (filename without extension)
            if file in self.exclude or directory in self.exclude or name in self.exclude:
                if self.verbosity >= 1:
                    self.stdout.write('Fixture skipped (excluded: %s, matches %s)' %
                                      (self.exclude, [file, directory, name]))
            else:
                updated_fixtures.append(fixture_file)
        return updated_fixtures

<强>使用

$ python manage.py excluding_loaddata app/fixtures/* -e not_this_fixture