Djuix.io Docs

Djuix.io

Views in Djuix.io

In Django REST Framework, views are responsible for processing HTTP requests and returning appropriate responses. Djuix simplifies this process by leveraging the power of ModelViewSet while providing intuitive ways to customize and extend functionality.

ModelViewSet: The Foundation of Djuix.io Views

By default, Djuix creates views using ModelViewSet, a powerful feature of Django REST Framework. This approach automatically provides a full set of CRUD (Create, Read, Update, Delete) operations with minimal code setup.

Here's what you get out of the box with each ModelViewSet:

Instead of writing separate view functions for each of these operations, Djuix sets them up automatically when you create a view for your model.

Create View.mov

Custom Functions: Extending Beyond CRUD

While CRUD operations cover many use cases, you often need more specialized functionality. This is where Djuix's custom functions come into play. These allow you to easily extend your views without diving into complex Django code.

Types of Custom Functions

Djuix supports a variety of custom function types:

  1. GET: Retrieve records based on specific filters
  2. CHECK: Get a specific field's value from a record
  3. CHECK_COUNT: Count records matching a given filter
  4. CREATE: Create a new record with custom logic
  5. UPDATE: Modify existing records with custom conditions
  6. DELETE: Remove records based on custom criteria
  7. RETURN: Customize the response format
  8. ARITHMETIC: Perform calculations on field values
  9. IF_CONDITION: Implement conditional logic
  10. AGGREGATE: Perform database aggregations

Custom Operations.mov

Helpers

Djuix provides several helpers and value sources to make your custom functions more flexible and powerful.

Value Sources

When creating custom functions, you can draw data from various sources:

  1. Query Params: Access data from the URL's query string (after the '?')
  2. Request Body: Utilize data sent in the request body (typically JSON)
  3. Constant Value: Use predefined string or number values
  4. Boolean Value: Employ True or False values
  5. Kwargs: Access keyword arguments passed to the function
  6. Result Variables: Use results from previous operations in the same custom function

Screenshot 2024-10-22 at 19.36.42.jpg

Helpers and Values

Djuix provides several helpers and value sources to make your custom functions more flexible and powerful.

Value Sources

When creating custom functions, you can draw data from various sources:

  1. Query Params: Access data from the URL's query string (after the '?')
  2. Request Body: Utilize data sent in the request body (typically JSON)
  3. Constant Value: Use predefined string or number values
  4. Boolean Value: Employ True or False values
  5. Kwargs: Access keyword arguments passed to the function
  6. Result Variables: Use results from previous operations in the same custom function

Filter Operations

Djuix supports a range of filter operations, allowing for precise data retrieval and manipulation. Some key filter operations include:

These filter operations can be applied to various field types in to refine searches and data retrieval.

Screenshot 2024-10-22 at 19.34.57.jpg

These operations can be applied to various field types, allowing for refined searches and data retrieval. For a complete list of filter operations, refer to the Django documentation.

Chaining Operations

One of the most powerful features of Djuix's custom functions is the ability to chain operations. This allows you to create complex logic flows within a single custom function.

Here's how chaining works:

  1. Start with an initial method (e.g., GET)
  2. Use the result of that operation in subsequent operations (e.g., UPDATE or AGGREGATE)
  3. Continue chaining operations as needed
  4. End with a RETURN operation to send the final result

Example of Chained Operations: Updating a Patient Record

Let's walk through a practical example of chained operations in a custom function. We'll create a custom PUT method to update a patient's record, demonstrating how Djuix allows you to build complex logic with ease.

Objective

Override the default PUT method to update a patient's record based on the request body, but only if the patient ID exists.

Step-by-Step Breakdown

  1. CHECK: Verify patient id

    First, we need to check if the patient ID exists in our database.

    [email protected]

    In this step:

    This operation will return a boolean value: True if the patient exists, False otherwise.

  2. IF_CONDITION: Handle Patient Existence

  3. RETURN:

Viewing the Generated Code

After setting up your custom function, you can view the generated code to understand how Djuix translates your GUI actions into Django code.

Screenshot 2024-10-23 at 14.16.51.jpg

This shows how Djuix translates your GUI-based custom function into a Django view method.

We can get to do more scallable custom operations like this involving GET CHECK AGGREGATE etc. Check out our tutorial for more usage

Best Practices for Custom Functions

  1. Start Simple: Begin with basic operations and gradually add complexity
  2. Use Descriptive Names: Name your custom functions clearly to indicate their purpose
  3. Leverage Value Sources: Utilize different value sources to make your functions dynamic
  4. Always Return: Ensure your custom function ends with a RETURN operation to provide a response