how to add functionality in django admin without a django model?

时间:2015-07-31 20:22:38

标签: django

I am bit new to django and I want to add a feature in django admin so that admin can fill a simple form to send sms via sms gateway. here is the form

class sendSms(forms.Form):
    allCategory = []
    for each in categories.objects.all():
         allCategory.append((each.id,each.category_name))
    allCategory = tuple(allCategory)

    allSubcategory = []
    for each in subcategory.objects.all():
        allSubcategory.append((each.id, each.subcategory_name))
    allSubcategory = tuple(allSubcategory)

    allCity = []
    for each in city.objects.all():
        allCity.append((each.id,each.city_name))
    allCity = tuple(allCity)

    category = forms.Select(choices=allCategory)
    subcategories = forms.Select(choices=allSubcategory)
    cities = forms.Select(choices=allCity)
    message = forms.TextInput()

I don't want to save the form data in a model so i haven't created a model for it. how can I add this form to django admin and the process the returned data?

thanks

1 个答案:

答案 0 :(得分:1)

A long time ago (on Django 1.3.1) I did an admin functionality without a model, but documentation on the subject was sparse at best at this time.

Things certainly have changed since then, but I can try to share what I came up with (roughly)

The structure is:

spim/
├── __init__.py
├── admin.py
├── models.py
├── requirements.txt
├── settings.py
├── templates
│   └── spim
│       ├── adm.container.html
│       └── inc.container_thumbs.html
├── templatetags
│   └── __init__.py
├── urls.py
├── utils.py
└── widgets.py

__init__.py

Empty file.

admin.py

I create a fake Meta and a fake "database model". I subclass django.contrib.admin.ModelAdmin to make the entries in the administration panel and register them.

Example:

class FakeMeta:
        abstract            = not True
        app_label           = __name__.split('.')[-2]
        module_name         = None
        verbose_name        = None
        verbose_name_plural = None
        get_ordered_objects = list
        def get_add_permission(self):    return 'add_%s'    % self.module_name
        def get_change_permission(self): return 'change_%s' % self.module_name
        def get_delete_permission(self): return 'delete_%s' % self.module_name
        def __init__(self, name, verbose_name=None, verbose_plural=None):
                self.module_name         = name
                self.verbose_name        = verbose_name or name
                self.verbose_name_plural = verbose_plural or '%ss'%self.verbose_name

class Image:
        _meta = FakeMeta('image')

class AdminImage(admin.ModelAdmin):
        # see django.contrib.admin.options.ModelAdmin.get_urls()

        def changelist_view(self, request, extra_context=None):
                # ...
        def add_view(self, request, form_url='', extra_context=None):
                # ...
        # ...

django.contrib.admin.site.register([Image], AdminImage)

models.py

class Image(object):
        def __init__(self, filename, original=None):
                # ...
        def save(self):
                # ...
        def get_absolute_url(self):
                # ...
        # ...

the end

Basically that's all for me, others files are not revelant to your subject.

Let me know if this might work in your setup and if you need more code to read.

PS: I'm curious to see if it can be done differently today.