Skip to content

Commit

Permalink
Update models.py - Referencing the User model (#127)
Browse files Browse the repository at this point in the history
* Update models.py - Referencing the User model

Referencing the User model as described in the django documentation https://docs.djangoproject.com/en/4.2/topics/auth/customizing/#referencing-the-user-model . This ensures that projects working with custom User models will also function correctly, and I believe this is considered best practice for referencing the user model in Django.

* Fix up tests to use usermodel

* User instead of UserModel, following consistent pattern for model naming

Co-authored-by: Özgür <ozgurturkiye@gmail.com>

* Remove spurious lines

* Update catalog/tests/test_views.py

* Update catalog/tests/test_views.py

---------

Co-authored-by: Hamish Willee <hamishwillee@gmail.com>
Co-authored-by: Özgür <ozgurturkiye@gmail.com>
  • Loading branch information
3 people authored Nov 3, 2023
1 parent 3202d5a commit 0fcfea3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 2 additions & 2 deletions catalog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __str__(self):
import uuid # Required for unique book instances
from datetime import date

from django.contrib.auth.models import User # Required to assign User as a borrower
from django.conf import settings # Required to assign User as a borrower


class BookInstance(models.Model):
Expand All @@ -74,7 +74,7 @@ class BookInstance(models.Model):
book = models.ForeignKey('Book', on_delete=models.RESTRICT, null=True)
imprint = models.CharField(max_length=200)
due_back = models.DateField(null=True, blank=True)
borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
borrower = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True)

@property
def is_overdue(self):
Expand Down
4 changes: 3 additions & 1 deletion catalog/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ def test_lists_all_authors(self):
from django.utils import timezone

from catalog.models import BookInstance, Book, Genre, Language
from django.contrib.auth.models import User # Required to assign User as a borrower

# Get user model from settings
from django.contrib.auth import get_user_model
User = get_user_model()

class LoanedBookInstancesByUserListViewTest(TestCase):

Expand Down

0 comments on commit 0fcfea3

Please sign in to comment.