Followers and Following:
This part we need to actually import “Follow” function.
I did some tests yesterday, and that note includes some related techniques:
https://www.yuque.com/akiencore/qgdgg5/ksg9eu
import new features and models:
- in /profiles/models.py
- add FollowerRelation
- add 3 variables in Profile ```python class FollowerRelation(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) profile = models.ForeignKey(“Profile”, on_delete=models.CASCADE) timestamp = models.DateTimeField(auto_now_add=True)
class Profile(models.Model):
# A OneToOneField is essentially the same as a ForeignKeyuser = models.OneToOneField(User, on_delete=models.CASCADE)location = models.CharField(max_length=220, null=True, blank=True) # allow null and blankbio = models.TextField(blank=True, null=True)# newtimestamp = models.DateTimeField(auto_now_add=True)updated = models.DateTimeField(auto_now=True)followers = models.ManyToManyField(User, related_name="following", blank=True)"""project_obj = Profile.objects.first()project_obj.followers.all() -> All users following this profileusers.following.all() -> All users I follow"""
- becuase we have changed the model, makemigration and migrate as usual```bash[root@localhost Twittme]# python3 ./manage.py makemigrationsYou are trying to add the field 'timestamp' with 'auto_now_add=True' to profile without a default; the database needs something to populate existing rows.1) Provide a one-off default now (will be set on all existing rows)2) Quit, and let me add a default in models.pySelect an option: 1Please enter the default value now, as valid PythonYou can accept the default 'timezone.now' by pressing 'Enter' or you can provide another value.The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.nowType 'exit' to exit this prompt[default: timezone.now] >>> timezone.nowMigrations for 'profiles':profiles/migrations/0002_auto_20200729_2241.py- Add field followers to profile- Add field timestamp to profile- Add field updated to profile- Create model FollowerRelation[root@localhost Twittme]# python3 ./manage.py migrateOperations to perform:Apply all migrations: admin, auth, contenttypes, profiles, sessions, tweetsRunning migrations:Applying profiles.0002_auto_20200729_2241... OK
test case:
- we simulate a following step in test file
- in /profiles/tests.py
- setup 2 users, run 2 tests cases
- make sure 2 corresponding profiles are created
- add a follower to a user, and check their following status ```python from django.test import TestCase
- setup 2 users, run 2 tests cases
Create your tests here.
from django.contrib.auth import get_user_model from .models import Profile
User = get_user_model()
class ProfileTestCase(TestCase): def setUp(self): self.user = User.objects.create_user( username=’cfe’, password=’somepassword’) self.userb = User.objects.create_user( username=’cfe-2’, password=’somepassword2’)
def test_profile_created_via_signal(self):qs = Profile.objects.all()self.assertEqual(qs.count(), 2) # the count of profiles is 2def test_following(self):first = self.usersecond = self.userbfirst.profile.followers.add(second) # first is followed by secondsecond_user_following_whom = second.following.all() # check who second is followingqs = second_user_following_whom.filter(user=first) # check is second following firstfirst_user_following_no_one = first.following.all()self.assertTrue(qs.exists()) # second should now following firstself.assertFalse(first_user_following_no_one.exists()) # first should now not following anyone
run test case for only "profiles" app:```bash[root@localhost Twittme]# python3 ./manage.py test profilesCreating test database for alias 'default'...System check identified no issues (0 silenced)...----------------------------------------------------------------------Ran 2 tests in 1.159sOKDestroying test database for alias 'default'...
all 2 cases passed.
