API: Djangoid

📖 To learn more, take a look at our complete API

📤 Example Request

  1. Generate an api key below👇

  2. Install node-fetch & add the GOOEY_API_KEY to your environment variables.
    Never store the api key in your code and don't use direcly in the browser.

$ npm install node-fetch
$ export GOOEY_API_KEY=sk-xxxx
  1. Use this sample code to call the API.
    If you encounter any issues, write to us at [email protected] and make sure to include the full code snippet and the error message.
import fetch from 'node-fetch';

const payload = {
  "input_prompt": "Can I do it with database constraints?",
  "messages": [
    {
      "role": "user",
      "content": "I have a many-to-many relationship from a team to its users. Some teams are \"personal\" teams and can't have members. How do I validate this at the DB level or app validation level to ensure that personal teams can't have more than 1 member (its owner!)."
    },
    {
      "role": "assistant",
      "content": "To enforce the rule that \"personal\" teams can't have more than one member (just the owner) in a many-to-many relationship between teams and users, you can achieve this validation at both the **database level** and the **application level**.\n\n### 1. **Application-Level Validation**\nYou can add validation logic in your Django model using the `clean()` method or a custom validator. For example, you can check the number of members when adding or updating a team to ensure that personal teams have a maximum of one member.\n\nHere\u2019s how you can do it:\n\n```python\nfrom django.core.exceptions import ValidationError\nfrom django.db import models\n\nclass Team(models.Model):\n    name = models.CharField(max_length=255)\n    is_personal = models.BooleanField(default=False)  # Field to mark personal teams\n    members = models.ManyToManyField('auth.User', related_name='teams')\n\n    def clean(self):\n        # Validate that personal teams can't have more than 1 member\n        if self.is_personal and self.members.count() > 1:\n            raise ValidationError(\n                \"Personal teams can't have more than one member.\"\n            )\n        super().clean()  # Call the parent clean method\n\n    def save(self, *args, **kwargs):\n        # Call the clean method during save to apply validation\n        self.clean()\n        super().save(*args, **kwargs)\n```\n\nIn this example:\n- The `is_personal` field is used to distinguish personal teams from others.\n- The `clean()` method checks if the team is marked as personal and if the number of members exceeds one. If so, it raises a `ValidationError`.\n\nThis validation will be triggered when saving the model instance, ensuring that personal teams cannot have more than one member.\n\n### 2. **Database-Level Validation**\nWhile Django doesn\u2019t directly support enforcing complex constraints like \"maximum members in a many-to-many relationship\" at the database level, you can still implement this using database triggers or constraints outside Django.\n\nHowever, Django does have **signals** that you can use to intercept attempts to add members to a personal team and prevent it from happening:\n\n```python\nfrom django.db.models.signals import m2m_changed\nfrom django.core.exceptions import ValidationError\n\ndef prevent_more_than_one_member(sender, **kwargs):\n    if kwargs['instance'].is_personal:\n        if kwargs['instance'].members.count() > 1:\n            raise ValidationError(\"Personal teams can't have more than one member.\")\n\nm2m_changed.connect(prevent_more_than_one_member, sender=Team.members.through)\n```\n\nThis approach uses the `m2m_changed` signal to listen for changes in the many-to-many relationship. Before adding any additional members, it checks if the team is personal and whether the number of members exceeds one.\n\n### Conclusion\n- **Application-level validation** using the `clean()` method ensures that personal teams cannot have more than one member.\n- **Database-level validation** can be partially achieved using Django's signals like `m2m_changed`. However, for stricter database-level constraints, you may need to resort to database triggers or custom SQL queries.\n\nBoth of these methods ensure that your business logic is respected and that personal teams maintain only their owner as a member."
    }
  ]
};

async function gooeyAPI() {
  const response = await fetch("https://test.api.gooey.ai/v2/video-bots?example_id=84mx4k6pa5qj", {
    method: "POST",
    headers: {
      "Authorization": "bearer " + process.env["GOOEY_API_KEY"],
      "Content-Type": "application/json",
    },
    body: JSON.stringify(payload),
  });

  if (!response.ok) {
    throw new Error(response.status);
  }

  const result = await response.json();
  console.log(response.status, result);
}

gooeyAPI();
  1. Generate an api key below👇

  2. Install requests & add the GOOEY_API_KEY to your environment variables.
    Never store the api key in your code.

$ python3 -m pip install requests
$ export GOOEY_API_KEY=sk-xxxx
  1. Use this sample code to call the API.
    If you encounter any issues, write to us at [email protected] and make sure to include the full code snippet and the error message.
import os
import requests

payload = {
    "input_prompt": "Can I do it with database constraints?",
    "messages": [
        {
            "role": "user",
            "content": "I have a many-to-many relationship from a team to its users. Some teams are \"personal\" teams and can't have members. How do I validate this at the DB level or app validation level to ensure that personal teams can't have more than 1 member (its owner!).",
        },
        {
            "role": "assistant",
            "content": "To enforce the rule that \"personal\" teams can't have more than one member (just the owner) in a many-to-many relationship between teams and users, you can achieve this validation at both the **database level** and the **application level**.\n\n### 1. **Application-Level Validation**\nYou can add validation logic in your Django model using the `clean()` method or a custom validator. For example, you can check the number of members when adding or updating a team to ensure that personal teams have a maximum of one member.\n\nHere’s how you can do it:\n\n```python\nfrom django.core.exceptions import ValidationError\nfrom django.db import models\n\nclass Team(models.Model):\n    name = models.CharField(max_length=255)\n    is_personal = models.BooleanField(default=False)  # Field to mark personal teams\n    members = models.ManyToManyField('auth.User', related_name='teams')\n\n    def clean(self):\n        # Validate that personal teams can't have more than 1 member\n        if self.is_personal and self.members.count() > 1:\n            raise ValidationError(\n                \"Personal teams can't have more than one member.\"\n            )\n        super().clean()  # Call the parent clean method\n\n    def save(self, *args, **kwargs):\n        # Call the clean method during save to apply validation\n        self.clean()\n        super().save(*args, **kwargs)\n```\n\nIn this example:\n- The `is_personal` field is used to distinguish personal teams from others.\n- The `clean()` method checks if the team is marked as personal and if the number of members exceeds one. If so, it raises a `ValidationError`.\n\nThis validation will be triggered when saving the model instance, ensuring that personal teams cannot have more than one member.\n\n### 2. **Database-Level Validation**\nWhile Django doesn’t directly support enforcing complex constraints like \"maximum members in a many-to-many relationship\" at the database level, you can still implement this using database triggers or constraints outside Django.\n\nHowever, Django does have **signals** that you can use to intercept attempts to add members to a personal team and prevent it from happening:\n\n```python\nfrom django.db.models.signals import m2m_changed\nfrom django.core.exceptions import ValidationError\n\ndef prevent_more_than_one_member(sender, **kwargs):\n    if kwargs['instance'].is_personal:\n        if kwargs['instance'].members.count() > 1:\n            raise ValidationError(\"Personal teams can't have more than one member.\")\n\nm2m_changed.connect(prevent_more_than_one_member, sender=Team.members.through)\n```\n\nThis approach uses the `m2m_changed` signal to listen for changes in the many-to-many relationship. Before adding any additional members, it checks if the team is personal and whether the number of members exceeds one.\n\n### Conclusion\n- **Application-level validation** using the `clean()` method ensures that personal teams cannot have more than one member.\n- **Database-level validation** can be partially achieved using Django's signals like `m2m_changed`. However, for stricter database-level constraints, you may need to resort to database triggers or custom SQL queries.\n\nBoth of these methods ensure that your business logic is respected and that personal teams maintain only their owner as a member.",
        },
    ],
}

response = requests.post(
    "https://test.api.gooey.ai/v2/video-bots?example_id=84mx4k6pa5qj",
    headers={
        "Authorization": "bearer " + os.environ["GOOEY_API_KEY"],
    },
    json=payload,
)
assert response.ok, response.content

result = response.json()
print(response.status_code, result)
  1. Generate an api key below👇

  2. Install curl & add the GOOEY_API_KEY to your environment variables.
    Never store the api key in your code.

export GOOEY_API_KEY=sk-xxxx
  1. Run the following curl command in your terminal.
    If you encounter any issues, write to us at [email protected] and make sure to include the full curl command and the error message.
curl 'https://test.api.gooey.ai/v2/video-bots?example_id=84mx4k6pa5qj' \
  -H "Authorization: bearer $GOOEY_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "input_prompt": "Can I do it with database constraints?",
  "messages": [
    {
      "role": "user",
      "content": "I have a many-to-many relationship from a team to its users. Some teams are \"personal\" teams and can'"'"'t have members. How do I validate this at the DB level or app validation level to ensure that personal teams can'"'"'t have more than 1 member (its owner!)."
    },
    {
      "role": "assistant",
      "content": "To enforce the rule that \"personal\" teams can'"'"'t have more than one member (just the owner) in a many-to-many relationship between teams and users, you can achieve this validation at both the **database level** and the **application level**.\n\n### 1. **Application-Level Validation**\nYou can add validation logic in your Django model using the `clean()` method or a custom validator. For example, you can check the number of members when adding or updating a team to ensure that personal teams have a maximum of one member.\n\nHere\u2019s how you can do it:\n\n```python\nfrom django.core.exceptions import ValidationError\nfrom django.db import models\n\nclass Team(models.Model):\n    name = models.CharField(max_length=255)\n    is_personal = models.BooleanField(default=False)  # Field to mark personal teams\n    members = models.ManyToManyField('"'"'auth.User'"'"', related_name='"'"'teams'"'"')\n\n    def clean(self):\n        # Validate that personal teams can'"'"'t have more than 1 member\n        if self.is_personal and self.members.count() > 1:\n            raise ValidationError(\n                \"Personal teams can'"'"'t have more than one member.\"\n            )\n        super().clean()  # Call the parent clean method\n\n    def save(self, *args, **kwargs):\n        # Call the clean method during save to apply validation\n        self.clean()\n        super().save(*args, **kwargs)\n```\n\nIn this example:\n- The `is_personal` field is used to distinguish personal teams from others.\n- The `clean()` method checks if the team is marked as personal and if the number of members exceeds one. If so, it raises a `ValidationError`.\n\nThis validation will be triggered when saving the model instance, ensuring that personal teams cannot have more than one member.\n\n### 2. **Database-Level Validation**\nWhile Django doesn\u2019t directly support enforcing complex constraints like \"maximum members in a many-to-many relationship\" at the database level, you can still implement this using database triggers or constraints outside Django.\n\nHowever, Django does have **signals** that you can use to intercept attempts to add members to a personal team and prevent it from happening:\n\n```python\nfrom django.db.models.signals import m2m_changed\nfrom django.core.exceptions import ValidationError\n\ndef prevent_more_than_one_member(sender, **kwargs):\n    if kwargs['"'"'instance'"'"'].is_personal:\n        if kwargs['"'"'instance'"'"'].members.count() > 1:\n            raise ValidationError(\"Personal teams can'"'"'t have more than one member.\")\n\nm2m_changed.connect(prevent_more_than_one_member, sender=Team.members.through)\n```\n\nThis approach uses the `m2m_changed` signal to listen for changes in the many-to-many relationship. Before adding any additional members, it checks if the team is personal and whether the number of members exceeds one.\n\n### Conclusion\n- **Application-level validation** using the `clean()` method ensures that personal teams cannot have more than one member.\n- **Database-level validation** can be partially achieved using Django'"'"'s signals like `m2m_changed`. However, for stricter database-level constraints, you may need to resort to database triggers or custom SQL queries.\n\nBoth of these methods ensure that your business logic is respected and that personal teams maintain only their owner as a member."
    }
  ]
}'

🎁 Example Response

{4 Items
"id"
:
string
"ohrsxzbjz3ev"
"url"
:
string
"https://test.gooey.ai/copilot/"
"created_at"
:
string
"2025-02-28T16:56:05.929708+00:00"
"output"
:
{14 Items
"final_prompt"
:
[4 Items
0
:
{
}2 Items
1
:
{
}2 Items
2
:
{
}2 Items
3
:
{
}2 Items
]
"output_text"
:
[1 Items
0
:
string
"The provided search results do not contain informa"
]
"output_audio"
:
NULL
"output_video"
:
NULL
"raw_input_text"
:
string
"Can I do it with database constraints?"
"raw_tts_text"
:
NULL
"raw_output_text"
:
[1 Items
0
:
string
"The provided search results do not contain informa"
]
"references"
:
[2 Items
0
:
{
}4 Items
1
:
{
}4 Items
]
"final_search_query"
:
string
"You previously asked about enforcing validation at"
"final_keyword_query"
:
NULL
"output_documents"
:
NULL
"reply_buttons"
:
NULL
"finish_reason"
:
[1 Items
0
:
string
"stop"
]
"metrics"
:
NULL
}
}

Please Login to generate the $GOOEY_API_KEY