Django: Custom "add only" inline -
i want inline form show fields contents, , not let users edit or remove entries, add them. means values similar when using readonly_fields
option, , "add ..." link @ bottom make form appear, letting users add more entries.
the can_delete
option it's useful here, readonly_fields
lock both add , change possibilities. imagine building new inline template do. in case, how show field values each entry , put form @ bottom?
edit: got until now:
# models.py class abstractmodel(models.model): user = models.foreignkey(user, editable = false) ... more fields ... class meta: abstract = true class parentmodel(abstractmodel): ... fields ... class childmodel(abstractmodel): parent = models.foreignkey(parentmodel, ... options ...) ... fields ... # admin.py class childmodelinline(admin.tabularinline): model = childmodel form = childmodelform can_delete = false class parentmodeladmin(admin.modeladmin): ... options ... inlines = (childmodelinline,) # forms.py class childmodelform(models.modelform): user = forms.charfield(required = false) ... more fields , stuff needed ... def __init__(self, *args, **kwargs): super(childmodelform, self).__init__(*args, **kwargs) try: user = user.objects.get(id = self.instance.user_id) except: return none self.fields['user'].initial = user.first_name self.fields['user'].widget.attrs['readonly'] = 'readonly'
in example i'm doing wanted user
field readonly.
in last line, if change widget attribute ['disabled'] = true
, works fine, need text entry, not disabled form field. i'm aware i'll need override save_model()
, save_formsets()
work properly.
i use extra=1
last working form.
then loop through forms except last 1 in view , change, this, every field: in django form, how make field readonly (or disabled) cannot edited?
you don't have in __init__
, can access attributes after entire formset created of course.
Comments
Post a Comment