26👍
✅
I think I found my answer.
First I tried:
field.widget = field.hidden_widget
which didn’t work.
The correct way happens to be:
field.widget = field.hidden_widget()
4👍
Can also use
def __init__(self, instance, *args, **kwargs):
super(FormClass, self).__init__(instance=instance, *args, **kwargs)
if instance and instance.item:
del self.fields['field_for_item']
👤oak
- [Django]-Django 1.7 – App 'your_app_name' does not have migrations
- [Django]-Removing 'Sites' from Django admin page
- [Django]-The QuerySet value for an exact lookup must be limited to one result using slicing. Filter error
4👍
def __init__(self, *args, **kwargs):
is_video = kwargs.pop('is_video')
is_image = kwargs.pop('is_image')
super(ContestForm, self).__init__(*args, **kwargs)
if is_video:
del self.fields['video_link']
# self.exclude('video_link')
if is_image:
del self.fields['image']
use delete
instead of self.exclude()
.
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found
- [Django]-Combining Django F, Value and a dict to annotate a queryset
- [Django]-Django-allauth: Linking multiple social accounts to a single user
0👍
You are coding this in the Form. Wouldn’t it make more sense to do this using CSS and JavaScript in the template code? Hiding a field is as easy as setting “display=’none'” and toggling it back to ‘block’, say, if you need to display it.
Maybe some context on what the requirement is would clarify this.
- [Django]-Getting Values of QuerySet in Django
- [Django]-Django/DRF – 405 Method not allowed on DELETE operation
- [Django]-Is this the right way to do dependency injection in Django?
Source:stackexchange.com