API

Functions

export_as_csv

See also

Are you looking for the Export as CSV action? .

adminactions.api.export_as_csv()

Exports a queryset as csv from a queryset with the given fields.

Defaults

Warning

Due a mistake the default configuration of export_as_csv is not csv but semicolon-csv

csv_options_default = {'date_format': 'd/m/Y',
                       'datetime_format': 'N j, Y, P',
                       'time_format': 'P',
                       'header': False,
                       'quotechar': '"',
                       'quoting': csv.QUOTE_ALL,
                       'delimiter': ';',
                       'escapechar': '\\', }

Usage examples

Returns HttpResponse:

response = export_as_csv(User.objects.all())

Write to file

>>> users = export_as_csv(User.objects.all(), out=open('users.csv', 'w'))
>>> users.close()

Write to buffer

>>> users = export_as_csv(User.objects.all(), out=StringIO())
>>> with open('users.csv', 'w') as f:
        f.write(users.getvalue())

Export with callable

>>> fields = ['username', 'get_full_name']
>>> export_as_csv(User.objects.all(), fields=fields, out=sys.stdout)
"sax";"FirstName 9 LastName 9"
"user_0";"FirstName 0 LastName 0"
"user_1";"FirstName 1 LastName 1"

Export with dictionaries

>>> fields = ['codename', 'content_type__app_label']
>>> qs = Permission.objects.filter(codename='add_user').values('codename', 'content_type__app_label')
>>> __ = export_as_csv(qs, fields=fields, out=sys.stdout)
"add_user";"auth"

export_as_xls

Please see the changelog

Exports a queryset as csv from a queryset with the given fields.

merge

See also

See Merge Records action for addtional notes.

Merge ‘other’ into master.

fields is a list of fieldnames that must be readed from other to put into master. If fields is None master will get all the other values except primary_key. Finally other will be deleted and master will be preserved

Custom validation

If you need to disable validation for some fields, it is possible to set parameter merge_form to a subclass of :class:adminactions.merge.MergeForm and change the validation there.

class CompanyMergeForm(merge.MergeForm):
    class Meta:
        model = models.Company
        fields = "__all__"

    def full_clean(self):
        super().full_clean()
        if 'address_psc' in self._errors:
            del self._errors['address_psc']

class CompanyAdmin(city_admin_mixin_generator(admin.ModelAdmin):
    form = CompanyForm
    merge_form = CompanyMergeForm

Filename callbacks

To use custom names for yours exports simply implements get_export_<TYPE>_filename in your Modeladmin class, these must return a string that will be used as filename in the SaveAs dialog box of the browser

example:

class UserAdmin(ModelAdmin):
    def get_export_as_csv_filename(request, queryset):
        if 'isadmin' in request.GET
            return 'administrators.csv'
        else:
            return 'all_users.csv'

Available callbacks:

  • get_export_as_csv_filename
  • get_export_as_fixture_filename
  • get_export_delete_tree_filename

Utils

adminactions.utils.clone_instance(instance, fieldnames=None)[source]
returns a copy of the passed instance.
Parameters:instancedjango.db.models.Model instance
Returns:django.db.models.Model instance
adminactions.utils.get_field_by_path(model, field_path)[source]

get a Model class or instance and a path to a attribute, returns the field object

Parameters:
Returns:

django.db.models.Field

>>> from django.contrib.auth.models import Permission
>>> p = Permission(name='perm')
>>> f = get_field_by_path(Permission, 'content_type')
>>> print f
auth.Permission.content_type
>>> p = Permission(name='perm')
>>> f = get_field_by_path(p, 'content_type.app_label')
>>> print f
contenttypes.ContentType.app_label
adminactions.utils.get_field_value(obj, field, usedisplay=True, raw_callable=False)[source]

returns the field value or field representation if get_FIELD_display exists

Parameters:
Returns:

field value

>>> from django.contrib.auth.models import Permission
>>> p = Permission(name='perm')
>>> print get_field_value(p, 'name')
perm
adminactions.utils.get_verbose_name(model_or_queryset, field)[source]

returns the value of the verbose_name of a field

typically used in the templates where you can have a dynamic queryset

Parameters:
Returns:

translated field verbose name

Return type:

unicode

Valid uses:

>>> from django.contrib.auth.models import User, Permission
>>> user = User()
>>> p = Permission()
>>> print unicode(get_verbose_name(user, 'username'))
username
>>> print unicode(get_verbose_name(User, 'username'))
username
>>> print unicode(get_verbose_name(User.objects.all(), 'username'))
username
>>> print unicode(get_verbose_name(User.objects, 'username'))
username
>>> print unicode(get_verbose_name(User.objects, user._meta.get_field_by_name('username')[0]))
username
>>> print unicode(get_verbose_name(p, 'content_type.model'))
python model class name
>>> get_verbose_name(object, 'aaa')
Traceback (most recent call last):
...
ValueError: `get_verbose_name` expects Manager, Queryset or Model as first parameter (got <type 'type'>)

Templatetags

adminactions.templatetags.actions.verbose_name(model_or_queryset, field)[source]

templatetag wrapper to adminactions.utils.get_verbose_name

adminactions.templatetags.actions.field_display(obj, field)[source]

returns the representation (value or get_FIELD_display()) of a field

see adminactions.utils.get_field_value