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:
Document.doctype
after adding similar doctype field in OptionCategory
) - preferably with ajax?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.
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 .
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With