Ask Documentation
WebsiteGitHubPyPI
  • Introduction
  • Getting Started
    • Install & Get Started
    • Hello, World!
  • Routes & Requests
    • Routes
    • Defining Routes
    • Request Data
    • HTTP Status Codes & Methods
    • CORS
  • Response
    • JSON Response
    • JSON Response With an HTTP Status Code
  • Classes
    • Class Instance Variable
    • Initialization/Constructor Method
  • Data Types
    • Dictionaries
  • Built-in Utilities
    • Quick_set()
    • Deep()
    • Serialize()
    • Require_keys()
    • Random Generators
    • Pattern Matching
    • Email
  • Database
    • Ask and Databases
    • Models/Classes
      • Columns
      • Initialization/Constructor
      • Serialization
      • The &basic decorator
    • CRUD
      • Add
      • Select
      • Update
      • Delete
    • Check if a Row Exists
    • Sorting
    • Database Lists
  • JWT Authentication
    • Introduction
    • Protecting Routes
    • How to Create a Basic Login System
    • Properties & Methods of _auth
    • Making Requests to Protected Routes
  • Decorators
    • What are Decorators?
    • Create and Use Custom Decorators
    • Built-in Decorators
  • Security
    • Hashing
    • Route Security
    • Environment Variables
  • Configuring the Transpiler
  • Askfile.toml
  • Modules & Libraries
    • Importing an Ask Module
    • Includes
    • Importing Python Modules
  • Development Tools
    • Editor Syntax Highlighting
    • Automatic API documentation
    • CLI Flags
    • Running in development mode
    • Versioning System
  • Contribute
    • Feature Requests
    • Bug Reports
    • Contribute Code
Powered by GitBook
On this page
  • Create a DB List
  • Properties of the List Object
Export as PDF
  1. Database

Database Lists

Store a list-like object in the database

Storing lists in the context of databases can be challenging. The easiest way to overcome this is to restructure your project to use multiple models and link them together with ids. Though this method might still be a bit complex. So, to solve this problem Ask has built-in database models ready to go for storing a list (list-like) objects.

The built-in database model is called List and it can be used along with the database column data type list_id (which is just an alias for int, but it improves readability) to link it in a table.

Create a DB List

my_list = db.list([1, 2, 3, 4])  # The passed in list is optional.

This will automatically also add the row to the database.

Properties of the List Object

  • Basically a serialization method.

  • Returns a standard list.

  • Note! Don't mutate the return value of this method, it won't update the list in the database.

Example:

my_list = db.list(['a', 'b', 'c'])
my_list.list()  # ['a', 'b', 'c']
  • Takes in an integer.

  • Returns a single item from the list by index number.

Example:

my_list = db.list(['a', 'b', 'c'])
my_list.get(1)  # 'b'
  • Append to the list.

Example:

my_list = db.list(['a', 'b', 'c'])
my_list.push('d')
my_list.list()  # ['a', 'b', 'c', 'd']
  • Remove an element from the list.

  • Takes in an integer.

  • Removes by index number.

Example:

my_list = db.list(['a', 'b', 'c'])
my_list.remove(1)
my_list.list()  # ['a', 'c']
  • Overwrite the list with a new one.

  • Takes in a list.

Example:

my_list = db.list(['a', 'b', 'c'])
my_list.list()  # ['a', 'b', 'c']
my_list.set_list([1, 2, 3])
my_list.list()  # [1, 2, 3]
PreviousSortingNextIntroduction

Last updated 4 years ago