Sandbox to test Generic relationships in DRF vs multiple ForeignKeys
NOTE: This project is for research purposes only. Don't use in production, use it with caution.
git clone https://github.com/gnud/generic_model_sandbox
cd generic_model_sandbox
virtualenv -p python3 venv
. venv/bin/activate
pip install -r requirements.txt
./manage.py migrate
./manage.py loaddata generic_app/fixtures/full_data.json
./manage.py shell -c "from django.contrib.auth.models import User; User.objects.create_superuser('root', '[email protected]', 'password')"
Run server
./manage.py runserver
Demo credentials:
user: root
pass: password
Single model named Comment having 2 sub models: Article, Post for the content. Content is retrieved and automatically determined via RelatedField.
DRF API that outputs content field object: URL: GET /api/app/v1/comment/
[
{
"pk": 1,
"content": null
},
{
"pk": 2,
"content": {
"pk": 2,
"content": "Post2"
}
},
{
"pk": 3,
"content": {
"pk": 3,
"content": "article3"
}
}
]
Model named CommentNormal having 2 sub models: Article, Post using 2 separate related fields, with rules one has to be null. Content object is determined by serializer method by hand, poorly.
DRF API that outputs content field object: URL: GET /api/app/v1/comment2/
[
{
"pk": 1,
"content": {
"pk": 1,
"content": "article1"
}
},
{
"pk": 2,
"content": {
"pk": 1,
"content": "Post1"
}
},
{
"pk": 3,
"content": {
"pk": 1,
"content": "Post1"
}
}
]