Хочу добавить функционал добавления комментариев к странице пользователя(как это сделано в Steam). Но при попытке это сделать ловлю ошибку "NOT NULL constraint failed: bboard_reviews.profile_id"
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, verbose_name="Пользователь")
first_name = models.CharField(max_length=20, db_index=True, verbose_name="Имя", null=True)
last_name = models.CharField(max_length=20, db_index=True, verbose_name="Фамилия", null=True)
email = models.EmailField(db_index=True, verbose_name="Email", null=True)
about = models.TextField(db_index=True, null=True, blank=True, verbose_name="О себе")
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
class Meta:
verbose_name = "Пользователь"
verbose_name_plural = "Пользователи"
def __str__(self):
return str(self.user)
class Reviews(models.Model):
"""Отзывы"""
name = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Пользователь")
text = models.TextField(verbose_name="Сообщение", max_length=5000)
profile = models.ForeignKey(Profile, verbose_name="Пользователь", on_delete=models.CASCADE)
def __str__(self):
return f"{self.name} - {self.profile}"
class Meta:
verbose_name = "Отзыв"
verbose_name_plural = "Отзывы"
forms.py
class ReviewsForm(ModelForm):
"""Форма отзывов"""
class Meta:
model = Reviews
fields = ('text', )
view.py
class AddReviews(View):
"""Отзывы"""
def post(self, request, pk):
form = ReviewsForm(request.POST)
profile = Profile.objects.get(id=pk)
if form.is_valid():
form = form.save(commit=False)
form.name = request.user
form.post = profile
form.save()
return redirect('index')
def profile_view(request, pk):
profile = Profile.objects.get(user_id=pk)
my_post = Bb.objects.filter(user_id=pk)
comments = Response.objects.filter(name_id=pk)
context = {'profile': profile, 'my_post': my_post, 'comments': comments}
return render(request, 'bboard/profile.html', context)
url.py
urlpatterns = [
path('reviews/<int:pk>/', AddReviews.as_view(), name='add_reviews'),
]
ну и сама форма отправки и вывод комментариев
<div>
{% for comm in comments %}
<p>Пользователь: <a href="{% url 'profile_view' comm.name.pk %}">{{ comm.name }}</a></p>
<p>Текст: {{ comm.text }}</p>
<br>
{% endfor %}
</div>
<h2>Отзывы о пользователе</h2>
<div class="contact-single">
<h3 class="editContent" style="outline: none; cursor: inherit;">Оставить отзыв</h3>
<form action="{% url 'add_reviews' profile.pk %}" method="post" class="mt-4">
{% csrf_token %}
<div class="form-group editContent">
<label for="contactcomment" class="editContent">
Ваш комментарий *
</label>
<textarea class="form-control border" rows="5" name="text"
id="contactcomment" required=""></textarea>
</div>
<button type="submit"
class="mt-3 btn btn-success btn-block py-3">
Отправить
</button>
</form>
</div>