python - How to include change_view form with value -
i have book
table there 1 filed value
contains encrypted title
, descriptions
of books.
now doing admin function there admin can view or edit user's submitted title , descriptions.
so far drive value field in admin/users/book/5/
using custom modelform
, here want show user's submitted value
using extra_context
know that's not showing in field.
models.py
class book(models.model): values = models.charfield(max_length=800, null=true, blank=true)
forms.py
class bookform(forms.modelform): extra_field = forms.charfield() def save(self, commit=true): value = self.cleaned_data.get('value', none) class meta: model = book
admin.py
class bookadmin(admin.modeladmin): def add_view(self, request, form_url='', extra_context=none): return super(bookadmin, self).add_view(request) def change_view(self, request, object_id, form_url='',): bookinfo = book.objects.get(pk=object_id) pprint.pprint(bookinfo.values) extra_context = {} extra_context['value'] = bookinfo.values pprint.pprint(extra_context) return super(bookadmin, self).change_view(request, object_id, form_url, extra_context=extra_context) form = bookform fieldsets = ( (none, { 'fields': ('value',), }), ) admin.site.register(book, bookadmin)
here mention changelist_view . tried don't know how include extra_context in book_change_form.html
so word providing initial values custom fields based on instance data
include below code in forms.py work ....
def __init__(self, *args, **kwargs): instance = kwargs.get('instance') if instance: pprint.pprint(instance.values) self.base_fields['values'].initial = instance.values forms.modelform.__init__(self, *args, **kwargs)
Comments
Post a Comment