-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #796 from liam-hq/devin/1740714523-add-django-docs
docs: add Django ORM documentation
- Loading branch information
Showing
1 changed file
with
82 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
frontend/apps/docs/content/docs/parser/supported-formats/django.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: Django ORM | ||
--- | ||
|
||
Django ORM is supported through PostgreSQL integration. You can use Django's ORM to define your database models and then extract the schema using pg_dump for use with Liam ERD. | ||
|
||
## Using Django ORM with Liam ERD | ||
|
||
1. Set up a Django project with PostgreSQL as the database backend: | ||
|
||
```python | ||
# settings.py | ||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.postgresql', | ||
'NAME': 'your_database_name', | ||
'USER': 'your_database_user', | ||
'PASSWORD': 'your_database_password', | ||
'HOST': 'localhost', | ||
'PORT': '5432', | ||
} | ||
} | ||
``` | ||
|
||
2. Define your models using Django's ORM: | ||
|
||
```python | ||
# models.py | ||
from django.db import models | ||
|
||
class Author(models.Model): | ||
name = models.CharField(max_length=100) | ||
email = models.EmailField(unique=True) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
class Post(models.Model): | ||
title = models.CharField(max_length=200) | ||
content = models.TextField() | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='posts') | ||
|
||
def __str__(self): | ||
return self.title | ||
``` | ||
|
||
3. Apply migrations to create the database schema: | ||
|
||
```bash | ||
python manage.py makemigrations | ||
python manage.py migrate | ||
``` | ||
|
||
4. Extract the schema using pg_dump: | ||
|
||
```bash | ||
pg_dump --schema-only --no-privileges --no-owner --file=schema.sql postgres://username:password@localhost:5432/your_database_name | ||
``` | ||
|
||
5. Use Liam CLI to build an ER diagram: | ||
|
||
```bash | ||
npx @liam-hq/cli erd build --format postgres --input schema.sql | ||
``` | ||
|
||
## Sample Implementation | ||
|
||
You can find a sample implementation of Django ORM with Liam ERD on GitHub: | ||
|
||
- GitHub Actions: [.github/workflows/django-with-postgres.yml](https://github.com/liam-hq/liam-erd-samples/blob/main/.github/workflows/django-with-postgres.yml) | ||
- Django project: [samples/django-with-postgres](https://github.com/liam-hq/liam-erd-samples/tree/main/samples/django-with-postgres) | ||
|
||
The sample project demonstrates how to: | ||
- Set up a Django project with PostgreSQL | ||
- Define models using Django's ORM | ||
- Extract the schema using pg_dump | ||
- Use the extracted schema with Liam ERD | ||
|
||
## Under the Hood | ||
|
||
Django ORM generates SQL for PostgreSQL, which is then parsed by Liam ERD using the PostgreSQL parser. For more details about PostgreSQL support, see the [PostgreSQL documentation](/docs/parser/supported-formats/postgresql). |