简体   繁体   中英

Remove form field from views with initial data

This is my view:

key = request.GET['key']
if key:
    tier_type = ContentType.objects.get_for_model(Listing)
    add_form = ProductPermissionForm(initial={'tier_type': tier_type,
                                              'tier_id': Listing.objects.get(listing_number=key).id},)
    form = add_form.as_p()
return HttpResponse(form)

I want to remove a form field from this ProductPermissionForm. Any ideas? I tried with init but it resets the form and my initial data is lost!

This is my init:

def __init__(self, key=None, *args, **kwargs):
    super(ProductPermissionForm, self).__init__(*args, **kwargs)
    if key is not None:
        self.fields.pop('lock')

lock is my field name I want to remove

Please try this:

def __init__(self, key=None, *args, **kwargs):
    super(ProductPermissionForm, self).__init__(*args, **kwargs)
    if key is not None:
        del self.fields['lock']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM