models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(upload_to='profile_images/', blank=True, null=True, verbose_name='Profile Picture')
bio = models.TextField(blank=True, null=True)
following = models.ManyToManyField(
'self',
related_name="followers",
symmetrical=False,
blank=True
)
def follow(self, user):
self.following.add(user)
def unfollow(self, user):
self.following.remove(user)
def is_following(self, user):
return self.following.filter(pk=user.pk).exists()
def __str__(self):
return "{}".format(self.user.username)
# def save(self, *args, **kwargs):
# super(Profile, self).save(*args, **kwargs)
class Blog(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=200)
description = models.TextField(blank=True, null=True)
blog_image = models.ImageField(upload_to='blog_images/', blank=True, null=True, verbose_name='Blog Picture')
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "{} {} {}".format(self.author, self.title, self.date)
class Meta:
ordering = ["date"]
forms.py
class BlogForm(forms.ModelForm):
class Meta:
model = Blog
fields = ('title', 'description', 'blog_image')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('profile_image', 'bio', 'following')
views.py
class CreateProfile(LoginRequiredMixin, CreateView):
model = Profile
form_class = ProfileForm
context_object_name = 'create_profile'
template_name = 'blog/create_profile.html'
success_url = reverse_lazy('home')
def form_valid (self, form):
form = ProfileForm(self.request.POST, self.request.FILES)
instance = form.save(commit=False)
instance.save()
form.save_m2m() # Save the ManyToManyField 'following'
return super(CreateProfile, self).form_valid(form)