Skip to content

Commit

Permalink
Merge pull request talkpython#25 from tbrlpld/django-rest-user-field
Browse files Browse the repository at this point in the history
Hide user field in api front-end and set default
  • Loading branch information
bbelderbos authored Jan 4, 2020
2 parents 9628a78 + d9ee51b commit 85b09a1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions days/069-072-django-rest/demo/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

class QuoteSerializer(serializers.ModelSerializer):

# This is not included in the videos. Without this setting, it was possible
# to set the user value to something other than the currently logged-in
# user. This setting hides the user field from the form in the API frontend
# and sets the currently logged-in users as the field value by default.
# See also: https://stackoverflow.com/a/53193276
user = serializers.HiddenField(default=serializers.CurrentUserDefault())

class Meta:
model = Quote
fields = ('quote', 'author', 'source', 'cover', 'user')
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,40 @@ <h2>Sign up today!</h2>
<div class="mui-textfield">
{{ form.username.label }}
{{ form.username }}
<div class="mui--text-danger">
{% for error in form.username.errors %}
{{ error }}
{% endfor %}
</div>
</div>
<div class="mui-textfield">
{{ form.email.label }}
{{ form.email }}
<div class="mui--text-danger">
{% for error in form.email.errors %}
{{ error }}
{% endfor %}
</div>
</div>
<div class="mui-textfield">
{{ form.password1.label }}
{{ form.password1 }}
<div class="mui--text-danger">
{% for error in form.password1.errors %}
{{ error }}
{% endfor %}
</div>
</div>
<div class="mui-textfield">
{{ form.password2.label }}
{{ form.password2 }}
<div class="mui--text-danger">
{% for error in form.password2.errors %}
{{ error }}
{% endfor %}
</div>
</div>
<!-- {{ form.as_p }} -->

<input class="mui-btn mui-btn--primary" type="submit" value="{% trans 'Submit' %}" />
</form>
Expand Down
4 changes: 2 additions & 2 deletions days/069-072-django-rest/demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
django
django-registration
django==2.2
django-registration==3.0.1
djangorestframework
requests
django-rest-swagger
Binary file modified days/069-072-django-rest/starter_code.zip
Binary file not shown.

0 comments on commit 85b09a1

Please sign in to comment.