Skip to content

Instantly share code, notes, and snippets.

@jbbarth
Last active May 28, 2018 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbbarth/10207e2cf9ee0dab0fd5d248af18a15c to your computer and use it in GitHub Desktop.
Save jbbarth/10207e2cf9ee0dab0fd5d248af18a15c to your computer and use it in GitHub Desktop.
...
{% if form.can_edit_captain_field() %}
<input type=checkbox name=is_captain id=id_is_captain {% if object.is_captain %}checked{% endif %}>
{% else %}
...
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