I currently have a comment functionality and I want to add a limit of one comment for each user but don't know how to do that.
I tought on making a 'posted' field in the user model which would be true when the user posted a comment but I don't know how to do that and most importantly if that is the better way of doing that...
class Comment(models.Model):
service = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True,)
content = models.CharField(max_length=200, null=False, blank=True)
date = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('product-feedback', kwargs={'pk': self.pk})
class ProductFeedbackView(DetailView):
model = Product
template_name = 'services/product-feedback.html'
def get_context_data(self , **kwargs):
data = super().get_context_data(**kwargs)
connected_comments = Comment.objects.filter(service=self.get_object())
number_of_comments = connected_comments.count()
data['comments'] = connected_comments
data['no_of_comments'] = number_of_comments
data['comment_form'] = CommentForm()
return data
def post(self , request , *args , **kwargs):
if self.request.method == 'POST':
comment_form = CommentForm(self.request.POST)
if comment_form.is_valid():
content = comment_form.cleaned_data['content']
new_comment = Comment(content=content, author=self.request.user , service=self.get_object())
new_comment.save()
return redirect(self.request.path_info)
{% block content %}
{% if user.is_authenticated %}
<form action="" method="POST" id="main_form" class="comment_form">
<div>
<label for="comment">Type Comment here</label>
{{ comment_form.content }} {% csrf_token %} <input type="submit" value="Post"></div>
</div>
</form>
{% else %}
<h2>You need to Login to comment</h2>
{% endif %}
{% for comment in comments %}
<h3> <b>{{ comment.author }} : </b> {{ comment.content }}</h3>
{% endfor %}
{% endblock content %}
If you are really sure a user should post one and only one comment, you should use a OneToOneField. This way, the unique constraint shall be be automatically generated and the object easier to manage.
class Comment(models.Model):
service = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True, related_name='comments')
author = models.OneToOneField(User, on_delete=models.CASCADE, blank=True, null=True,)
content = models.CharField(max_length=200, null=False, blank=True)
If you want the user to post only one comment per product but still allow him to write comments on as many products as he wants, you should add a constraint in the Meta class :
class Comment(models.Model):
service = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True, related_name='comments')
author = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True,)
content = models.CharField(max_length=200, null=False, blank=True)
class Meta:
constraints = [
models.UniqueConstraint(fields=["service", "author"], name="One comment per user per product")
]
Related
I am trying to get a list of all categories and below each category the list of all related articles.
The problem is that I am getting the list of all articles under each category. I read documentation and few answers but nothing seems to be working and I am not sure where I am making the mistake.
models.py
class Category(models.Model):
title = models.CharField(max_length=75, default='', blank=False, null=False)
body = CharField(max_length=2000, default='', null=True, blank=True)
slug = models.SlugField(unique=True, blank=True)
publish = models.DateTimeField('publish', default=timezone.now)
class Meta:
ordering = ('-publish',)
verbose_name = 'category'
verbose_name_plural = 'categories'
get_latest_by = 'publish'
class Article(models.Model):
id = models.AutoField(primary_key=True)
author = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL) #settings INSTALLED_APPS
title = models.CharField('title', max_length=200)
body = CKEditor5Field('Body', config_name='extends')
last_updated = models.DateTimeField(auto_now=True)
publish = models.DateTimeField('publish', default=timezone.now)
#tags = TagField(required=False, widget=LabelWidget)
category = models.ForeignKey(Category, on_delete = models.CASCADE, blank=True, related_name='category')
slug = models.SlugField(unique=True, blank=True)
views.py
#login_required
def hal_home(request):
top_categories = Category.objects.all()[:3]
category_related_articles = Article.objects.prefetch_related('category').all()
context = {
'category_related_articles': category_related_articles
}
return render(request, 'hal/homepage.html', context)
homepage.html
{% for category in top_categories %}
<div class="btn btn-light text-center text-justify">
<div>{{ category.title }}</div>
</div>
<br>
<p>{% for article in category_related_articles %}
{{article.title}}<br>
{% endfor %}</p>
{% empty %}
<div>No categories</div>
{% endfor %}
You should use prefetch_related with your category queryset.
And also I would suggest you to change the related_name to articles since category.articles.all() seems more readable and easier than category.category.all() .
category = models.ForeignKey(Category, on_delete = models.CASCADE, blank=True, related_name='articles')
Now you can change some code in your views and templates.
top_categories = Category.objects.prefetch_related("articles").all()[:3]
context = {'top_categories': top_categories}
Now you can get articles by category in template with:
{% for category in top_categories %}
{{category.title}}
# articles of each category
{% for article in category.articles.all %}
{{article}}
{% endfor %}
{% endfor %}
Django: ValidationError ['ManagementForm data is missing or has been tampered with'] i have been getting this error when i use the forminline factory module, im sorry if my question isnt placed properly, this is my first time here.
my form template is this:
{% extends 'accounts/main-form.html'%}
{% load static %}
{% block title %}
<title>CRM | Form</title>
{% endblock %}
{% block link %}
<link rel="stylesheet" type="text/css" href="{% static 'css/style1.css'%}">
{% endblock %}
{% block content %}
<div class="container">
<form action="" method="POST">
{{formset.management_form}}
{% for i in formset %}
{{i}}
{% endfor %}
<input type="submit" class="btn" value="submit">
</form>
</div>
{% endblock%}
this is the form code in views.py
def order_form(request, pk):
customers = Customer.objects.get(id=pk)
OrderFormSet = inlineformset_factory(Customer, Order, fields=('product', 'status',))
formset = OrderFormSet(request.POST)
if request.method == 'POST':
formset = OrderFormSet(request.POST, instance=customers)
if formset.is_valid():
formset.save()
return redirect('/')
context = {'formset': formset}
return render(request, 'accounts/order_form.html', context)
my models
from django.db import models
# Create your models here.
class Customer(models.Model):
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Tag(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Product(models.Model):
CATEGORY = (
('Indoor', 'Indoor'),
('OutDoor', 'Outdoor')
)
name = models.CharField(max_length=200, null=True)
price = models.FloatField(null=True)
category = models.CharField(max_length=200, null=True, choices=CATEGORY)
description = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True)
tag = models.ManyToManyField(Tag)
def __str__(self):
return self.name
class Order(models.Model):
STATUS = (
('Pending', 'Pending'),
('Out for delivery', 'Out for delivery'),
('Delivered', 'Delivered')
)
customer = models.ForeignKey(Customer, null=True, on_delete= models.DO_NOTHING)
product = models.ForeignKey(Product, null=True, on_delete= models.DO_NOTHING)
date_created = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=200, null=True, choices=STATUS)
def __str__(self):
return self.product.name
my form.py
from django.forms import ModelForm
from .models import Order
class Order_form(ModelForm):
class Meta:
model = Order
fields = ['product', 'status']
In my Django App, I have 2 models. One called Post and one called Categories. When a user clicks on a category, I want only the posts that are in that category to appear in the category detail view. For example if a user clicks on the medical category, I only want the posts in the medical category to appear.
Models:
class Category(models.Model):
title = models.CharField(max_length=200)
colorcode = models.CharField(max_length=20, blank=True, null=True)
description = models.TextField()
image = models.ImageField(blank=True, null=True)
slug = models.SlugField(unique=True)
class Post(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
text = models.TextField()
sub_description = models.TextField(blank=True, null=True)
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
image = models.ImageField(blank=True, null=True)
live = models.BooleanField(default=False)
slug = models.SlugField(unique=True)
Views:
class CategoryDetailView(DetailView):
model = Category
def get_context_data(self, **kwargs):
context = super(CategoryDetailView, self).get_context_data(**kwargs)
context['category_posts'] = Post.objects.filter(live=True)
return context
Template:
{% for post in category_posts %}
<div class="post">
<div class="post-title">
{{ post.title }}
</div>
<div class="post-author">
{{ post.author }}
</div>
</div>
{% endfor %}
In a DetailView, you have access to the actual object being rendered (in your case the Category instance) through self.object.
So in your get_context_data method you can do:
context['category_posts'] = Post.objects.filter(live=True, category=self.object)
Note that self.object might be None so you may want to deal with that case.
I want to allow users to edit their own comments. I tried to compare comment.nick as well as to request.user.username and to request.user.get_username. For example comment.nick return 'author' and request.user.username will return 'author', but Edit won't be visible.
{% if request.user.username == comment.nick %}
Edit
{% endif %}
# models.py
class Commentary(models.Model):
nick = models.ForeignKey('Profile', on_delete=models.CASCADE, max_length=20)
comment = models.TextField(max_length=300)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
added = models.DateTimeField(auto_now_add=True, blank=True)
I have two models, Booking and Confirmation that are related via a ForeignKey relationship through "booking." I want to only display bookings in my detail view that have an attribute value of is_confirmed ==True. I don't really want a queryset, I just want to display the booking information from the "Booking" model if the confirmation is True in the template.
models.py:
class Booking(models.Model):
user = models.ForeignKey(CustomUser, null=True, default='', on_delete=models.CASCADE)
expert = models.ForeignKey(CustomUser, null=True, default='',on_delete=models.CASCADE, related_name='bookings')
title = models.CharField(max_length=200, default='Video call with ..', null=True)
start_time = models.DateTimeField('Start time', null=True)
end_time = models.DateTimeField('End time', null=True)
notes = models.TextField('Notes', blank=True, null=True)
class Meta:
verbose_name = 'Booking'
verbose_name_plural = 'Bookings'
def get_absolute_url(self):
return reverse('booking:booking_detail', kwargs={"pk": self.pk})
class Confirmation(models.Model):
booking = models.ForeignKey(Booking, on_delete=models.CASCADE)
expert_confirming = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
is_confirmed = models.BooleanField(default=False)
def get_absolute_url(self):
return reverse('booking:booking_detail', kwargs={"pk": self.booking_id})
views.py:
class BookingDetailView(DetailView):
model = Booking
template = 'templates/booking_detail.html'
booking_detail.html:
<div class="container" id="booking_content">
<p>{{ booking.title }}</p>
<p>{{ booking.start_time }}</p>
<p>Booking request by: {{ booking.user }}</p>
<p>Expert requested: {{ booking.expert }}</p></div>
I'm not sure how the if statement in the template should reference these related models to display what I want.
I think that with the way you have your models you would have to run a query on the Confirmation model to determine whether there exists a confirmation for a particular booking. But why have a separate confirmation model at all? Try just moving the relevant field into the booking model:
class Booking(models.Model):
user = models.ForeignKey(CustomUser, null=True, default='', on_delete=models.CASCADE)
expert = models.ForeignKey(CustomUser, null=True, default='',on_delete=models.CASCADE, related_name='bookings')
title = models.CharField(max_length=200, default='Video call with ..', null=True)
start_time = models.DateTimeField('Start time', null=True)
end_time = models.DateTimeField('End time', null=True)
notes = models.TextField('Notes', blank=True, null=True)
is_confirmed = models.BooleanField(default=False) # just this field since you already have an expert.
That simplifies things, and puts a little less load on your db. Then, you can show only bookings that are confirmed with this template language:
{% if booking.is_confirmed %}
<div class="container" id="booking_content">
<p>{{ booking.title }}</p>
<p>{{ booking.start_time }}</p>
<p>Booking request by: {{ booking.user }}</p>
<p>Expert requested: {{ booking.expert }}</p>
</div>
{% else %}
...
{% endif %}
You may have a good reason for having a separate confirmation model. If so, then this answer is irrelevant. If not, then maybe this could help simplify things for you.