I have made a feedback form. Now I want to make a user list of those who have given feedback on the particular product. My motive is, that if any user gives feedback on a particular product, he/she won't be able to give another feedback on that particular product and can't see the feedback form. A user can share just one feedback on one product. But he/she will be able to give feedback on other's products. How can I make a user list of those who have given feedback on the particular product? Help me...
models.py:
class Products(models.Model):
user = models.ForeignKey(User, related_name="merchandise_product_related_name", on_delete=models.CASCADE, blank=True, null=True)
product_title = models.CharField(blank=True, null=True, max_length = 250)
on_delete=models.CASCADE, blank=True, null=True)
def __str__(self):
return str(self.pk) "." str(self.product_title)
class ProductREVIEWS(models.Model):
user = models.ForeignKey(User, related_name='userREVIEW',on_delete=models.CASCADE)
product = models.ForeignKey(Products, related_name='productREVIEWrelatedNAME',on_delete=models.CASCADE)
def __str__(self):
return str(self.pk) "." str(self.product) "(" str(self.user) ")"
views.py:
def quick_view(request, quick_view_id):
quick_view = get_object_or_404(Products, pk=quick_view_id)
AllProductFeedback = quick_view.productREVIEWrelatedNAME.all()
TotalProductsFeedback = AllProductFeedback.count()
OverallFeedback = ProductREVIEWS.objects.all()
context = {
"quick_view":quick_view,
"TotalProductsFeedback":TotalProductsFeedback,
"AllProductFeedback":AllProductFeedback,
"OverallFeedback":OverallFeedback,
}
return render(request, 'quickVIEW_item.html', context)
CodePudding user response:
You can .filter(…)
[Django-doc] with:
User.objects.filter(userREVIEW__product=quick_view)
You can however simply let the database prevent creating two ProductREVIEWS
for the same user
and product
combination with a UniqueConstraint
[Django-doc]:
from django.conf import settings
class ProductReview(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name='reviews',
on_delete=models.CASCADE
)
product = models.ForeignKey(
Product,
related_name='reviews',
on_delete=models.CASCADE
)
class Meta:
constraints = [
models.UniqueConstraint(
fields=('user', 'product'),
name='review_once'
)
]
def __str__(self):
return f'{self.pk}.{self.product}({self.user})'
with the modified names, it is:
User.objects.filter(reviews__product=quick_view)
But here the database will thus reject a second review of the same product by the same user.
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: normally a Django model is given a singular name, so
Product
instead of.Products
Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from
toProductREVIEWS
ProductReview
.
Note: The
related_name=…
[Django-doc] is the name of the manager to fetch the related objects in reverse. Therefore normally therelated_name
of aForeignKey
orManyToManyField
is plural, for examplereviews
instead of.productREVIEWrelatedNAME