Filtering models by user or by items

71 views
Skip to first unread message

Shamaila Moazzam

unread,
Sep 1, 2016, 11:45:45 AM9/1/16
to django...@googlegroups.com
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)

M Hashmi

unread,
Sep 1, 2016, 11:58:53 AM9/1/16
to django...@googlegroups.com
What did you get with:
products = Product.objects.filter(account=user)
As it is inheriting user from get_shopaccount method.

Regards,
Mudassar

--
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...@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.

Shamaila Moazzam

unread,
Sep 1, 2016, 12:22:22 PM9/1/16
to Django users
@ Mudassar .Thanx for your reply but this query is not working.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

mandeep444

unread,
Sep 2, 2016, 5:50:41 AM9/2/16
to Django users

def get_shopproducts(self):
   account = self.get_shopaccount()
    ## this is the problem area..

  here i want to filter like
  products = Product.objects.filter(pk__in=[id for i.product in account ])
    return products
I hope this help you.

Shamaila Moazzam

unread,
Sep 2, 2016, 3:52:23 PM9/2/16
to django...@googlegroups.com
Thanks@Mandeep...i will apply this also and let you know
actually i have changed my models and have put seller as F.K in Product.
after that
def get_products(self):
account = self.get_account()
products = Product.objects.filter(seller=account)
self.products = products
return products..... 

regards.
shamaila

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/8zbsj_yUmlY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages