22π
You need to create a custom form and template for the LinkSectionInline
.
Something like this should work for the form:
LinkFormset = forms.modelformset_factory(Link)
class LinkSectionForm(forms.ModelForm):
def __init__(self, **kwargs):
super(LinkSectionForm, self).__init__(**kwargs)
self.link_formset = LinkFormset(instance=self.instance,
data=self.data or None,
prefix=self.prefix)
def is_valid(self):
return (super(LinkSectionForm, self).is_valid() and
self.link_formset.is_valid())
def save(self, commit=True):
# Supporting commit=False is another can of worms. No use dealing
# it before it's needed. (YAGNI)
assert commit == True
res = super(LinkSectionForm, self).save(commit=commit)
self.link_formset.save()
return res
(That just came off the top of my head and isnβt tested, but it should get you going in the right direction.)
Your template just needs to render the form and form.link_formset appropriately.
5π
Django-nested-inlines is built for just this. Usage is simple.
from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline
from models import A, B, C
class MyNestedInline(NestedTabularInline):
model = C
class MyInline(NestedStackedInline):
model = B
inlines = [MyNestedInline,]
class MyAdmin(NestedModelAdmin):
pass
admin.site.register(A, MyAdmin)
- [Django]-How Can I Disable Authentication in Django REST Framework
- [Django]-Naming convention for Django URL, templates, models and views
- [Django]-Django β Static file not found
1π
My recommendation would actually be to change your model. Why not have a ForeignKey
in Link
to LinkSection
? Or, if itβs not OneToMany, perhaps a ManyToMany
field? The admin interface will generate that for free. Of course, I donβt recommend this if links donβt logically have anything to do with link sections, but maybe they do? If they donβt, please explain what the intended organization is. (For example, is 3 links per section fixed or arbitrary?)
- [Django]-No module named MySQLdb
- [Django]-Which Stack-Overflow style Markdown (WMD) JavaScript editor should we use?
- [Django]-Django Rest Framework β Could not resolve URL for hyperlinked relationship using view name "user-detail"
0π
You can create a new class, similar to TabularInline or StackedInline, that is able to use inline fields itself.
Alternatively, you can create new admin templates, specifically for your model. But that of course overrules the nifty features of the admin interface.
- [Django]-CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False
- [Django]-Reducing Django Memory Usage. Low hanging fruit?
- [Django]-Django: Where to put helper functions?