Home > Back-end >  How can I set a logic like, "If a user order any product then the user will be able to give fee
How can I set a logic like, "If a user order any product then the user will be able to give fee

Time:08-06

My motive is to set a logic like, a user only will be able to give a product review on that product he/she bought. I tried this below way but didn't work.

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)
    
    def __str__(self):
        return str(self.pk)   "."   str(self.product_title)



class ProductOrder(models.Model):

    User = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='UserOrderRelatedName',on_delete=models.CASCADE)
    CustomerName = models.CharField(max_length=250, blank=True, null=True)
    Product = models.ForeignKey(Products, related_name='ProductOrderRelatedName',on_delete=models.CASCADE)
    ProductTitle = models.CharField(max_length=250, blank=True, null=True)

    def __str__(self):
        return f'{self.pk}.{self.User}({self.Product})'

views.py:

def quick_view(request, quick_view_id):

    quick_view = get_object_or_404(Products, pk=quick_view_id)

    context = {
        "quick_view":quick_view,
    }
        
    return render(request, 'quickVIEW_item.html', context)

urls.py:

path('quick_view/<int:quick_view_id>/', views.quick_view, name="quick_view"),

template:

  {% if quick_view in request.user.UserOrderRelatedName.all %}

            
    <form action="{% url 'feedBack' quick_view_id=quick_view.id %}" method="POST"  style="font-size: 13px;" novalidate="" autocomplete="off" enctype="multipart/form-data">
      {% csrf_token %}

      <textarea id="email" placeholder="Share your experiencs..." rows="10" style="font-size: 13px;" type="email"  name="feedBACK" value="" required></textarea>

          
      <button type="submit"  style="font-size: 13px;">
          Submit
      </button>

    </form>

  {% endif %}

CodePudding user response:

The problem is that User and Product are connected via ProductOrder, which makes it difficult to access (if not impossible) via the template reverse accessor without a for-loop. You should set a variable in your view and pass it to your template:

products_bought_by_user = Product.objects.filter(
    ProductOrderRelatedName__user=request.user
)
if quick_view in products_bought_by_user:
    user_has_bought_product = True 
else:
    user_has_bought_product = False
...

Then you can use it easily in your template:

{% if user_has_bought_product %}
# your form
{% endif %}

Furthermore, using the view means you can validate server-side that someone is able to post a review - at the moment it's just dependant on whether the HTML form is visible or not.

if user_has_bought_product:
    # process posted review
else:
    return redirect('access-denied-or-something')
  • Related