Skip to content

Commit

Permalink
Merge pull request #796 from liam-hq/devin/1740714523-add-django-docs
Browse files Browse the repository at this point in the history
docs: add Django ORM documentation
  • Loading branch information
sasamuku authored Feb 28, 2025
2 parents 42a82e8 + 6e91a69 commit 96a274a
Showing 1 changed file with 82 additions and 0 deletions.
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).

0 comments on commit 96a274a

Please sign in to comment.