Thursday, September 1, 2016

Re: Filtering models by user or by items

What did you get with:
products = Product.objects.filter(account=user)
As it is inheriting user from get_shopaccount method.

Regards,
Mudassar

On Thu, Sep 1, 2016 at 8:45 AM, Shamaila Moazzam <shamaila.moazzam@gmail.com> wrote:
am making a shops dashboard view .in that view i have used a mixin as mentioned below....my issue is i want to get products related to a specific account or user . i have products m2m in shop model and also have user f.k in Shop model. In get_shopproducts() function in ShopAccountMixin()..i am unable to filter products of a requested account..... please reply... shops/models.py


class Shop(models.Model):      user = models.ForeignKey(settings.AUTH_USER_MODEL)      product = models.ManyToManyField(Product)      ......      def __unicode__(self):          return str(self.user.username)        products/models.py      class Product(models.Model):          title = models.CharField(max_length=120)        description = models.TextField(blank=True, null=True)        price = models.DecimalField(decimal_places=2, max_digits=20)        publish_date = models.DateTimeField(auto_now=False,             auto_now_add=False,       ...         def __unicode__(self): #def __str__(self):             return self.title       shops/mixins.py       class ShopAccountMixin(LoginRequiredMixin, object):    def get_shopaccount(self):      user = self.request.user      shopaccount = Shop.objects.filter(user=user)      if shopaccount.exists():         return shopaccount      else:          return None     def get_shopproducts(self):      account = self.get_shopaccount()      ## this is the problem area..    here i want to filter like    products = Product.objects.all(account....???)???        return products        class shopsDashBoard(ShopAccountMixin, FormMixin, View):       model = Shop      form_class = SellerForm      template_name = "shops/seller.html"   def get(self, request, *args, **kwargs):        apply_form = self.get_form()      account = self.get_shopaccount()      exists = account      active = None      context = {}      if exists:          active = account.active          context["active"] = active      if not exists and not active:          context["title"] = "Apply for Account"          context["apply_form"] = apply_form      elif exists and not active:          context["title"] = "Account Pending"       elif exists and active:          context["title"] = "Shops Dashboard"            #products = Product.objects.filter(seller=account)      context["products"] = self.get_shopproducts()      return render(request, "shops/dashboard.html", context)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFimi6wB1Ycnz2On4EALzvM5b0M-a17sB4PXpKFA5KdCiFK4WQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANoUts7rWv7z6a096FUKe5dR9kcXTnyX4v2Ep7My2Uqpc4nD5w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment