0

how can I return a looped values in a method that no using array, for now i am using an array.

here is my code :

def get_ticket_sum_quantity(self, product_id, date_select):
    prod = Product.objects.get(id=product_id)
    sumOfQuantity = Ticket.objects.filter(date_select=date_select, product=prod).aggregate(Sum('quantity'))['quantity__sum']    
    if sumOfQuantity == None:
        sumOfQuantity = 0
    prodAvailable = prod.quantity - sumOfQuantity
    return prodAvailable 
def get_ticket_available_product(self, date_select, client_id, quantity):
    client = Client.objects.get(id=client_id)
    prodCount = Product.objects.filter(client=client_id,status='Active').values_list('id', flat=True)
    array = []
    for id in prodCount:
        prodAvailable = Ticket.objects.get_ticket_sum_quantity(id, date_select)
        prodAvailable = prodAvailable - quantity
        if prodAvailable < 0:
            data = {'id':id}
        else :
            data = {'id':id}
            data = data['id']
            array.append(data)
    return array

and when i used it and the out-put is...

Ticket.objects.get_ticket_available_product('2011-12-29', 5, 1)
[3, 2, 6, 1]

my question is, is there any other options that I will be not using an array so that it will return like this?

3
2
6
1

1 Answer 1

2

Yes, you can use

  1. Either convert your function to a generator, so that after each invocation it returns the next value or Generates StopIteration if there is no value to return.

    def get_ticket_available_product(self, date_select, client_id, quantity):
        client = Client.objects.get(id=client_id)
        prodCount = Product.objects.filter(client=client_id,status='Active').values_list('id', flat=True)
        for id in prodCount:
            prodAvailable = Ticket.objects.get_ticket_sum_quantity(id, date_select)
            prodAvailable = prodAvailable - quantity
            if prodAvailable < 0:
                data = {'id':id}
            else :
                data = {'id':id}
                data = data['id']
                yield data
        return
    

Usage

data = get_ticket_available_product(self, date_select, client_id, quantity)
for d in data:
    print d
  1. Or You can also split the returned list in the manner desired

Example

print '\n'.join(str(i) for i in [3, 2, 6, 1])

or as in this case

print '\n'.join(str(i) for i in Ticket.objects.get_ticket_available_product('2011-12-29', 5, 1) )
  1. Or You can split the return value in this manner. Remember in that case the return value would be a string

        data = data['id']
            array.append(data)
    return '\n'.join(str(i) for i in array )
    

Usage:

print Ticket.objects.get_ticket_available_product('2011-12-29', 5, 1)

Btw:

What is the purpose of this code snippet?

if prodAvailable < 0:
    data = {'id':id}
Sign up to request clarification or add additional context in comments.

1 Comment

i use it if the prodAvialable is negative then it will not be included to be return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.