Skip to content

Add models.APIModel #1

Open
Open
@derks

Description

@derks

Would be handy to be able to access the api like a model:

myapi = drest.api.API('http://localhost:8000/api/v1/')
myapi.auth(user='john.doe', password='XXXXXXXX')

class UserModel(drest.models.APIModel):
    class Meta:
        api = myapi
        resource = 'users'
        path = '/users/'
        
# New user
user = UserModel()
user.first_name = 'John'
user.last_name = 'Doe'

# POST http://localhost:8000/api/v1/users/
user.save()

### Update user

# GET http://localhost:8000/api/v1/users/?username='john.doe'
user = UserModel(username='john.doe') # exception if more than 1 result, or none

user.first_name
>>> 'john'

user.last_name
>>> 'doe'

user.first_name = 'Bob'

user.first_name
>>> 'Bob'

# PUT http://localhost:8000/api/v1/users/14/
user.save()

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions