I want to pre-populate the values in the admin form from the URL parameters. For normal fields, this works by simply adding /? Field_name = parameter values in the URL. However, my field is located in the built-in admin form on the form. Is there a way to pass URL parameters through an inline form? For instance:
models.py:
class Parent(Model):
...
class Child(Model):
name = CharField(max_length=20)
provenance = ManyToManyField('Provenance')
class Provenance(Model):
child = ForeignKey(Child)
parent = ForeignKey(Parent)
admin.py:
class ProvenanceInline(admin.StackedInline):
model = Provenance
form = myforms.ProvenanceForm
...
class ChildAdmin(admin.ModelAdmin):
form = myforms.ChildForm
[inlines] = ProvenanceInline
...
Now I have a custom view for showing the Parent object, and in this view, I would like to have a link to create this Parent's Child. Theoretically this will work:
<a href="{%url 'admin:myapp_child_add'%}?parent={{origin.pk}}">Add Child</a>
The problem is that the “parent” field is hidden inside Formline InlineAdmin. How to make ProvenanceForm see a URL variable passed to ChildForm?
get_formset ProvenanceInline get_inline_instances ChildAdmin, . (BTW, ChildForm , request, . , self.request). !