Re: Django filters with Foreign Key

2016-09-19 Thread A.Khan

>
> Happy to see we are in same boat. It did work with like:
>

def prod_sold(self):
prod = self.get_prod().filter(seller__user=self.account.user)
sold = Order.objects.filter(id=products)
return sold
But it only shows number of products sold. 

I couldn't make it happen.

If you can find solution share with me as well and I will do the same.

Regards,
Ali 

-- 
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+unsubscr...@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/16d19efc-5511-4c0b-a3fa-633780ccbcc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django filters with Foreign Key

2016-09-19 Thread Shamaila Moazzam

Hi ,

i am having the same problem ..with my sellers App .

can you share your solutionthat how you filter 

regards

On Monday, September 19, 2016 at 5:27:31 PM UTC+5, A.Khan wrote:
>
> I just got it done.
>
> Thanks.
>
> On Mon, Sep 19, 2016 at 4:19 AM, Ali khan  > wrote:
>
>> I've tried many ways but it ends with the same error that says:
>>
>> Cannot query "alikhan": Must be "User" instance.
>>
>> Where alikhan is admin user I've created with createsuperuser.
>>
>> Hate to bother you again but please advise.
>>
>> Regards,
>> Ali
>>
>> On Sun, Sep 18, 2016 at 11:08 PM, James Schneider > > wrote:
>>
>>> On Sep 18, 2016 10:22 PM, "Ali khan" > 
>>> wrote:
>>> >
>>> > Thank you for your kind response James.
>>> >
>>> > I must be doing something wrong but I thought that importing different 
>>> models and assigning them with variable may had help me to filter it out. I 
>>> will try your suggestion first to save your valued time and then will post 
>>> with result.
>>> >
>>> > Let me add a field for the seller in my "Orders" model like "seller = 
>>> models.ForeignKey(Seller)" and then filter it out.
>>> > But I tried that before still as per your suggestion I will do that 
>>> again.
>>> > Thanks again.
>>> >
>>>
>>> Once you do that, you can filter the orders with something like this:
>>>
>>> orders = Order.objects.filter(seller__user=user) 
>>>
>>> Inside of the filter function you defined. The way you are currently 
>>> doing it, you'll end up with Seller objects rather than Order objects.
>>>
>>> -James
>>>
>>> -- 
>>> 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...@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/CA%2Be%2BciX6N3eQOnb5dP877WM0FEgj%2BAjt-QfT6anwE5jNMafx3Q%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+unsubscr...@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/c77f059c-5ab6-43e3-a3bc-099e1aefab57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django filters with Foreign Key

2016-09-19 Thread Ali khan
I just got it done.

Thanks.

On Mon, Sep 19, 2016 at 4:19 AM, Ali khan  wrote:

> I've tried many ways but it ends with the same error that says:
>
> Cannot query "alikhan": Must be "User" instance.
>
> Where alikhan is admin user I've created with createsuperuser.
>
> Hate to bother you again but please advise.
>
> Regards,
> Ali
>
> On Sun, Sep 18, 2016 at 11:08 PM, James Schneider  > wrote:
>
>> On Sep 18, 2016 10:22 PM, "Ali khan"  wrote:
>> >
>> > Thank you for your kind response James.
>> >
>> > I must be doing something wrong but I thought that importing different
>> models and assigning them with variable may had help me to filter it out. I
>> will try your suggestion first to save your valued time and then will post
>> with result.
>> >
>> > Let me add a field for the seller in my "Orders" model like "seller =
>> models.ForeignKey(Seller)" and then filter it out.
>> > But I tried that before still as per your suggestion I will do that
>> again.
>> > Thanks again.
>> >
>>
>> Once you do that, you can filter the orders with something like this:
>>
>> orders = Order.objects.filter(seller__user=user)
>>
>> Inside of the filter function you defined. The way you are currently
>> doing it, you'll end up with Seller objects rather than Order objects.
>>
>> -James
>>
>> --
>> 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+unsubscr...@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/ms
>> gid/django-users/CA%2Be%2BciX6N3eQOnb5dP877WM0FEgj%2BAjt-
>> QfT6anwE5jNMafx3Q%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+unsubscr...@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/CAAXvsYk0SN23WotK_ikG7foEHMKYYN7pLzrYLJmPtm-MVRYORw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django filters with Foreign Key

2016-09-19 Thread Ali khan
I've tried many ways but it ends with the same error that says:

Cannot query "alikhan": Must be "User" instance.

Where alikhan is admin user I've created with createsuperuser.

Hate to bother you again but please advise.

Regards,
Ali

On Sun, Sep 18, 2016 at 11:08 PM, James Schneider 
wrote:

> On Sep 18, 2016 10:22 PM, "Ali khan"  wrote:
> >
> > Thank you for your kind response James.
> >
> > I must be doing something wrong but I thought that importing different
> models and assigning them with variable may had help me to filter it out. I
> will try your suggestion first to save your valued time and then will post
> with result.
> >
> > Let me add a field for the seller in my "Orders" model like "seller =
> models.ForeignKey(Seller)" and then filter it out.
> > But I tried that before still as per your suggestion I will do that
> again.
> > Thanks again.
> >
>
> Once you do that, you can filter the orders with something like this:
>
> orders = Order.objects.filter(seller__user=user)
>
> Inside of the filter function you defined. The way you are currently doing
> it, you'll end up with Seller objects rather than Order objects.
>
> -James
>
> --
> 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+unsubscr...@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/CA%2Be%2BciX6N3eQOnb5dP877WM0FEgj%
> 2BAjt-QfT6anwE5jNMafx3Q%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+unsubscr...@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/CAAXvsYmSxGvDyJf0-SXeJ8eeC2Unqs5u9Ph7-3jnH4V0xSetCw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django filters with Foreign Key

2016-09-18 Thread James Schneider
On Sep 18, 2016 10:22 PM, "Ali khan"  wrote:
>
> Thank you for your kind response James.
>
> I must be doing something wrong but I thought that importing different
models and assigning them with variable may had help me to filter it out. I
will try your suggestion first to save your valued time and then will post
with result.
>
> Let me add a field for the seller in my "Orders" model like "seller =
models.ForeignKey(Seller)" and then filter it out.
> But I tried that before still as per your suggestion I will do that again.
> Thanks again.
>

Once you do that, you can filter the orders with something like this:

orders = Order.objects.filter(seller__user=user)

Inside of the filter function you defined. The way you are currently doing
it, you'll end up with Seller objects rather than Order objects.

-James

-- 
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+unsubscr...@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/CA%2Be%2BciX6N3eQOnb5dP877WM0FEgj%2BAjt-QfT6anwE5jNMafx3Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django filters with Foreign Key

2016-09-18 Thread Ali khan
Thank you for your kind response James.

I must be doing something wrong but I thought that importing different
models and assigning them with variable may had help me to filter it out. I
will try your suggestion first to save your valued time and then will post
with result.

Let me add a field for the seller in my "Orders" model like "seller =
models.ForeignKey(Seller)" and then filter it out.
But I tried that before still as per your suggestion I will do that again.
Thanks again.

Regards,
Ali


On Sun, Sep 18, 2016 at 1:14 PM, James Schneider 
wrote:

> On Sep 18, 2016 4:01 AM, "Ali khan"  wrote:
> >
> > I have two different apps and applying filters by ForeignKey and as
> newbie I am not able to understand how it works.
> >
> > My Order App:
> >   ORDER_STATUS_CHOICES={
> >   ('created','Created'),
> >   ('paid','Paid'),
> >   }
> >>
> >> class Order(models.Model):
> >> status = models.CharField(max_length=120,
> choices=ORDER_STATUS_CHOICES, default='created')
> >> cart = models.ForeignKey(Cart)
> >> user = models.ForeignKey(UserCheckout, null=True)
> >> billing_address = models.ForeignKey(UserAddress,
> related_name='billing_address', null=True)
> >> shipping_address = models.ForeignKey(UserAddress,
> related_name='shipping_address', null=True)
> >> shipping_total_price = models.DecimalField(max_digits=50,
> decimal_places=2, default=5.99)
> >>
> >> order_total = models.DecimalField(max_digits=50, decimal_places=2,
> )
> >> order_id = models.CharField(max_length=20, null=True, blank=True)
> >> paymethod = models.CharField(max_length=120, choices=CHOICES,
> default='CreditCard')
> >>
> >>
> >>   My seller's app:
> >>
> >>  class Seller(models.Model):
> >> user = models.ForeignKey(settings.AUTH_USER_MODEL)
> >> managers = models.ManyToManyField(settings.AUTH_USER_MODEL,
> related_name="manager_sellers", blank=True)
> >> active = models.BooleanField(default=False)
> >> timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
> >
> > I am trying to keep all the filters in mixin.py so I can call and use as
> per need. Following is the function and I can't filter orders by user who
> is the seller.
> >
> > def sold(self):
> > user = self.Seller.user
> > order = Seller.objects.filter(user=user)
> > return order
> >
>
> There is no relationship between the Order and the Seller, so you can't
> filter the Order's based on a specific Seller. You probably want to add a
> FK from Order to Seller.
>
> > Please advise.
> >
> > --
>
> I'm a bit confused. Are you trying to filter based on kwarg arguments
> captured through your urls.py, or via GET/POST parameters provided by a
> form, or just trying to limit the scope of available objects that a
> specific user can see?
>
> Everything you've shown here is a model (not an app). Filtering is
> generally performed either in the views, and/or in the model managers. Can
> you provide the views and possibly mixins?
>
> If you have existing filters that aren't working, please post up that code
> along with any trace backs.
>
> -James
>
> --
> 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+unsubscr...@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/CA%2Be%2BciWOPXqojqzt36j4pxL2G3AN2-
> R9MRJ9MecskLqkLhs%2BKg%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+unsubscr...@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/CAAXvsY%3Dpg55hgitx-vQsvmvukF%2BQtka_O5C-L2vQQGSQrWMMgg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django filters with Foreign Key

2016-09-18 Thread James Schneider
On Sep 18, 2016 4:01 AM, "Ali khan"  wrote:
>
> I have two different apps and applying filters by ForeignKey and as
newbie I am not able to understand how it works.
>
> My Order App:
>   ORDER_STATUS_CHOICES={
>   ('created','Created'),
>   ('paid','Paid'),
>   }
>>
>> class Order(models.Model):
>> status = models.CharField(max_length=120,
choices=ORDER_STATUS_CHOICES, default='created')
>> cart = models.ForeignKey(Cart)
>> user = models.ForeignKey(UserCheckout, null=True)
>> billing_address = models.ForeignKey(UserAddress,
related_name='billing_address', null=True)
>> shipping_address = models.ForeignKey(UserAddress,
related_name='shipping_address', null=True)
>> shipping_total_price = models.DecimalField(max_digits=50,
decimal_places=2, default=5.99)
>>
>> order_total = models.DecimalField(max_digits=50, decimal_places=2, )
>> order_id = models.CharField(max_length=20, null=True, blank=True)
>> paymethod = models.CharField(max_length=120, choices=CHOICES,
default='CreditCard')
>>
>>
>>   My seller's app:
>>
>>  class Seller(models.Model):
>> user = models.ForeignKey(settings.AUTH_USER_MODEL)
>> managers = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name="manager_sellers", blank=True)
>> active = models.BooleanField(default=False)
>> timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
>
> I am trying to keep all the filters in mixin.py so I can call and use as
per need. Following is the function and I can't filter orders by user who
is the seller.
>
> def sold(self):
> user = self.Seller.user
> order = Seller.objects.filter(user=user)
> return order
>

There is no relationship between the Order and the Seller, so you can't
filter the Order's based on a specific Seller. You probably want to add a
FK from Order to Seller.

> Please advise.
>
> --

I'm a bit confused. Are you trying to filter based on kwarg arguments
captured through your urls.py, or via GET/POST parameters provided by a
form, or just trying to limit the scope of available objects that a
specific user can see?

Everything you've shown here is a model (not an app). Filtering is
generally performed either in the views, and/or in the model managers. Can
you provide the views and possibly mixins?

If you have existing filters that aren't working, please post up that code
along with any trace backs.

-James

-- 
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+unsubscr...@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/CA%2Be%2BciWOPXqojqzt36j4pxL2G3AN2-R9MRJ9MecskLqkLhs%2BKg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Django filters with Foreign Key

2016-09-18 Thread Ali khan
I have two different apps and applying filters by ForeignKey and as newbie
I am not able to understand how it works.

My Order App:
  ORDER_STATUS_CHOICES={
  ('created','Created'),
  ('paid','Paid'),
  }

class Order(models.Model):
status = models.CharField(max_length=120, choices=ORDER_STATUS_CHOICES,
default='created')
cart = models.ForeignKey(Cart)
user = models.ForeignKey(UserCheckout, null=True)
billing_address = models.ForeignKey(UserAddress,
related_name='billing_address', null=True)
shipping_address = models.ForeignKey(UserAddress,
related_name='shipping_address', null=True)
shipping_total_price = models.DecimalField(max_digits=50,
decimal_places=2, default=5.99)

order_total = models.DecimalField(max_digits=50, decimal_places=2, )
order_id = models.CharField(max_length=20, null=True, blank=True)
paymethod = models.CharField(max_length=120, choices=CHOICES,
default='CreditCard')


  My seller's app:

 class Seller(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
managers = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name="manager_sellers", blank=True)
active = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)

I am trying to keep all the filters in mixin.py so I can call and use as
per need. Following is the function and I can't filter orders by user who
is the seller.

def sold(self):
user = self.Seller.user
order = Seller.objects.filter(user=user)
return order

Please advise.

-- 
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+unsubscr...@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/CAAXvsYnOOr-R5r9O3cnmUvAU68UGhRaGVWvYxAspaboms%3DY6%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.