GrantU
GrantU

Reputation: 6545

Django object has no attribute

I have a bulk Update. Each message created I need to call .send(gateway) This is what I have tried:

  objs = [                               
        Message(                           
            recipient_number=e.mobile,     
            content=content,               
            sender=e.contact_owner,        
            billee=user,                   
            sender_name=sender             
        ).send(gateway)                                  
        for e in query                     

    ]                                      
    # Send messages to DB                  
    Message.objects.bulk_create(objs)    

I get this error:

Task Request to Process with id 3ab72d3c-5fd8-4b7d-8cc5-e0400455334f raised exception: 'AttributeError("\'NoneType\' object has no attribute \'pk\'",)'

why?

Upvotes: 1

Views: 1563

Answers (1)

Paulo Almeida
Paulo Almeida

Reputation: 8061

You are creating the objs list by calling send on each element of query. Presumably, send does not return anything and you get a list of None. Try this:

objs = []
for element in query:
    message = Message(**kwargs)
    message.send(gateway)
    objs.append(message)

Message.objects.bulk_create(objs)

**kwargs is just a placeholder for all the parameters you pass to Message. You can use a dictionary or just pass all the parameters as in your original code.

As a side note, list comprehensions are usually indicated when you want a new list and not for side effects (like sending a message). Here you want both, so the for loop is appropriate.

Upvotes: 4

Related Questions