Last active
May 28, 2018 08:45
-
-
Save jbbarth/10207e2cf9ee0dab0fd5d248af18a15c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
{% if form.can_edit_captain_field() %} | |
<input type=checkbox name=is_captain id=id_is_captain {% if object.is_captain %}checked{% endif %}> | |
{% else %} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PirateForm(ModelForm): | |
def __init__(...): | |
self.user = user | |
... | |
if not self.can_edit_captain_field(): | |
del self.fields["is_captain"] | |
def can_edit_captain_field(self): | |
# NB: this condition is used both in the form object's __init__() method | |
# above and in the template. This is critical that "self.fields" stays in | |
# sync with the fields displayed in the HTML form, else it might cause | |
# nasty bugs where fields are silently reset. See BUG-1234. | |
return bool(self.user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment