Models

Defining Models

The Model class implements in an ORM-style the Pipedrive entities defined in *. Each entity class inherits from the Model class and defines its fields using class Field, relationships, and behaviors.

The basic definition of a model looks like this:

from pypipedrive.orm import Model
from pypipedrive.api import V2
from pypipedrive.orm import fields as F

class Entity(Model):

    id   = F.IntegerField("id", readonly=True)
    name = F.TextField("subject")

    class Meta:
        entity_name = "entity"
        version     = V2

The Meta class inside the model definition specifies the entity name as defined in the Pipedrive API and the API version to use (V1 or V2). The entity_name is used to build the API endpoints for the model.

Note

All entities are already defined in the models module. You can directly import and use them without redefining.

CRUD Operations

The Model class provides class methods and instance methods to perform CRUD operations on the Pipedrive entities.

  • Create: To create a new record, instantiate the model and call the save() method.

from pypipedrive.models import Deals

new_deal = Deals(title="New Deal", ...)
new_deal.save()
print(new_deal.id)  # The ID is assigned after saving
  • Retrieve: To retrieve a record by its ID, use the get() class method.

deal = Deals.get(id=1)
print(deal.title)
  • Update: To update an existing record, modify its attributes and call the save() method.

deal.title = "Updated Deal Title"
deal.save()
  • List: To list records with optional filtering, use the all() class method.

deals: List[Deals] = Deals.all(params={...})
for deal in deals:
    print(deal.id, deal.title)
  • Delete: To delete a record, call the delete() method on the instance.

deal.delete()
  • Batch delete: To delete multiple records at once, use the batch_delete() class method.

Deals.batch_delete(ids=[1, 2, 3])

These methods handle the necessary API calls and data serialization/deserialization automatically.

Other Model methods include:

  • Fetch: Refresh the instance data from the API.

deal.fetch()
  • Exists: Check if a record exists by its ID.

exists: bool = Deals.exists(id=1)
  • To record: Convert the instance to its dictionary representation using Pipedrive field names.

record_dict = deal.to_record()
  • From record: Create an instance from a dictionary representation.

record = {...}
deal = Deals.from_record(record)