Skip to content
Advertisement

How can I make a user list of those who have given feedback on the particular product?

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?

models.py:

JavaScript

views.py:

JavaScript

Advertisement

Answer

You can .filter(…) [Django-doc] with:

JavaScript

You can however simply let the database prevent creating two ProductREVIEWS for the same user and product combination with a UniqueConstraint [Django-doc]:

JavaScript

with the modified names, it is:

JavaScript

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 the User model [Django-doc] directly. For more information you can see the referencing the User 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 ProductREVIEWS to ProductReview.


Note: The related_name=… [Django-doc] is the name of the manager to fetch the related objects in reverse. Therefore normally the related_name of a ForeignKey or ManyToManyField is plural, for example reviews instead of productREVIEWrelatedNAME.

Advertisement