# Structure declaration

{% hint style="success" %}
This is the official documentation of the `forestadmin-agent-django` and `forestadmin-agent-flask` Python agents.
{% endhint %}

Creating a custom data source always starts with declaring the structure of the data

* Which collections are present?
* What fields do they contain?
* What are their types?

## Columns

### Examples

```python
from forestadmin.datasource_toolkit.collections import Collection

class MyCollection(Collection):
    def __init__(self, datasource):
        super().__init__("MyCollection", datasource)
        # [...]
        self.add_field("id", {
          "type": "Column",
          "column_type": "Number",
          "is_primary_key": True,
        })

        self.add_field("title", {
          "type": "Column",
          "column_type": "String",
          "validations": [{"operator": "present"}],
        })

        self.add_field("mpa_rating", {
          "type": "Column",
          "column_type": "Enum",
          "enum_values": ["G", "PG", "PG-13", "R", "NC-17"],
          "default_value": "G",
        })

        self.add_field("stars", {
          "type": "Column",
          "column_type": [{ "firstName": "String", "lastName": "String" }],
        })
```

### Typing

The typing system for columns is the same as the one used when declaring fields in the agent customization step.

You can read all about it in ["Under the hood > Data Model > Typing"](https://docs.forestadmin.com/developer-guide-agents-python/under-the-hood/data-model/typing).

### Validation

When using primitive type fields, Forest Admin supports declaring a validation clause, which will be imported into the UI of the admin panel to validate records before creating/updating them.

The API for validation is the same as with [condition trees](https://docs.forestadmin.com/developer-guide-agents-python/getting-started/queries/filters#condition-trees), besides the fact that there is no `"field"` entry.

```json
{
  "aggregator": "and",
  "conditions": [
    { "operator": "present" },
    { "operator": "like", "value": "found%" },
    { "operator": "today" }
  ]
}
```

## Relationships

{% hint style="warning" %}
Only **intra**-data source relationships should be declared at the collection level.

For **inter**-data source relationships, you should use [jointures at the customization step](https://docs.forestadmin.com/developer-guide-agents-python/agent-customization/relationships).
{% endhint %}

You can declare relationships at the collection level, but that means that the data source you are making is responsible for handling them.

This will work out of the box for data sources using the "local-cache" strategy, however, please read ["Intra-data source Relationships"](https://docs.forestadmin.com/developer-guide-agents-python/data-sources/custom/translation/relationships), before starting with the "query translation" strategy.

### Examples

```python
from forestadmin.datasource_toolkit.collections import Collection

class MyCollection(Collection):
    def __init__(self, datasource):
        super().__init__("MyCollection", datasource)
        # [...]
        self.add_field(
            "director",
            {
                "type": "ManyToOne",
                "foreign_collection": "people",
                "foreign_key": "directorId",
                "foreign_key_target": "id",
            },
        )

        self.add_field(
            "actors",
            {
                "type": "ManyToMany",
                "foreign_collection": "people",
                "through_collection": "actorsOnMovies",
                "origin_key": "movieId",
                "origin_key_target": "id",
                "foreign_key": "actorId",
                "foreign_key_target": "id",
            },
        )
```

### Typing

The typing system for relationships is the same as the one used when declaring jointures in the agent customization step.

You can read all about it in ["Under the hood > Data Model > Relationships"](https://docs.forestadmin.com/developer-guide-agents-python/under-the-hood/data-model/relationships).
