Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin form with predefined inlines

The problem is to have the inline models to have some of theirs fields preselected according to other model.

Let's make an example:

class Document(models.Model):
    DOC_TYPES = ((DC1, 'Doc type 1'), (DC2, 'Doc type 2'))
    doctype = model.CharField(choices=DOC_TYPES, default=DC1)

class OptionCategory(models.Model):
    name = model.CharField()

class Option(models.Model):
    document = models.ForeignKey(Document)
    option_category = models.ForeignKey(OptionCategory)
    some_data = models.CharField()

Assume that option is defined inline in document model admin.

OptionCategory is necessary - those may be added by admin - I'd like to have a creation form for Document with so many option models in inline forms as there is option categories. This can be achieved easily using extras in Option model admin. The questions are:

  1. How to present inline fields each with different option category preselected?
  2. How to change options dynamically (basing on Document.doctype after adding similar doctype field in OptionCategory) - preferably with ajax?
like image 966
phoenix Avatar asked Jan 16 '14 23:01

phoenix


People also ask

What is Inlines in django?

django-inline-actions provides a handy templatetag render_inline_action_fields , which adds these information as hidden fields to a form. As the action does not know that an intermediate form is used, we have to include some special handling.

Where is django admin?

The django-admin script should be on your system path if you installed Django via pip . If it's not in your path, ensure you have your virtual environment activated. Generally, when working on a single Django project, it's easier to use manage.py than django-admin .

What is the purpose of the admin site in a Django project?

The Django admin application can use your models to automatically build a site area that you can use to create, view, update, and delete records. This can save you a lot of time during development, making it very easy to test your models and get a feel for whether you have the right data.

What is admin panel in django?

One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site. The admin's recommended use is limited to an organization's internal management tool.


1 Answers

  1. How to present inline fields each with different option type preselected:

models.py

    class DocType(models.Model):
        name = models.CharField(max_length=255)

        def __unicode__(self):
            return self.name


    class Document(models.Model):
        doc_type = models.ForeignKey(DocType)
        some_data = models.CharField(max_length=255)

        def __unicode__(self):
            return self.some_data


    class OptionType(models.Model):
        name = models.CharField(max_length=255)
        doc_type = models.ForeignKey(DocType)

        def __unicode__(self):
            return self.name


    class Option(models.Model):
        document = models.ForeignKey(Document)
        option_type = models.ForeignKey(OptionType)
        some_data = models.CharField(max_length=255)

        def __unicode__(self):
            return self.some_data

admin.py

 class InlineOption(admin.TabularInline):
    model = Option
    extra = 3

    initial = [
        {'some_data': 'init_val1', 'option_type': 1},
        {'some_data': 'init_val2'},
    ]

    create_from_default = True

    def get_formset(self, request, obj=None, **kwargs):
        initial = self.initial[:]

        class _Form(forms.ModelForm):
            form_initial = initial

            def __init__(self, *args, **kwargs):
                if len(self.form_initial) and not 'instance' in kwargs:
                    kwargs['initial'] = self.form_initial.pop(0)
                return super(_Form, self).__init__(*args, **kwargs)

        if self.create_from_default:
            if request.method == 'GET':
                self.form = _Form
            else:
                self.form = forms.ModelForm
        else:
            self.form = _Form

        return super(InlineOption, self).get_formset(request, obj, **kwargs)



class AdminDocument(admin.ModelAdmin):
    inlines = [InlineOption]
like image 99
vadimchin Avatar answered Sep 29 '22 03:09

vadimchin