By Emmanuelle Delescolle
# my_app/serializers.py
from .models import MyModel
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = '__all__'
# my_app/views.py
from .models import MyModel
from .serializers import MyModelSerializer
class MyModelViewSet(viewsets.ModelViewSet):
serializer_class = MyModelSerializer
queryset = MyModel.objects.all()
# urls.py
from my_app.views import MyModelViewSet
router.register(r'my_app/my_models', MyModelViewSet)
# my_app/admin.py
from .models import MyModel
admin.site.register(MyModel)
# my_app/endpoints.py
from .models import MyModel
router.register(MyModel)
# my_app/models.py
class Category(models.Model):
name = models.Charield(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category,
related_name='products',
on_delete=models.CASCADE)
description = models.TextField(blank=True,
null=True)
image = models.ImageField(null=True)
price = models.DecimalField(max_digits=5,
decimal_places=2)
/* app/models/myapp/category.js */
export default Model.extend({
name: attr('string'),
products: hasMany('myapp/product', {
async: true,
inverse: 'category',
}),
});
/* app/models/myapp/product.js */
export default Model.extend({
name: attr('string'),
description: attr('string'),
image: attr('string'),
price: attr('number'),
category: belongsTo('myapp/category', {
async: true,
inverse: 'products',
}),
});
/* app/models/myapp/product.js */
export default Model.extend({
name: attr('string'),
description: attr('string'),
image: attr('string'),
price: attr('number'),
category: belongsTo('myapp/category', {
async: true,
inverse: 'products',
}),
});
{
"fields": [
{"required": false, "label": "Id", "widget": "number", "name": "id", "readonly": true},
{"required": true, "label": "Name", "widget": "text", "name": "name", "readonly": false},
{"required": true, "extra": {"related_model": "myapp/category"}, "label": "Category",
"widget": "foreignkey", "name": "category", "readonly": false},
{"required": true, "label": "Price", "widget": "number", "name": "price", "readonly": false},
{"required": false, "label": "Picture", "widget": "image", "name": "picture", "readonly": false},
{"required": false, "label": "Description", "widget": "text", "name": "description", "readonly": false},
{"required": false, "label": "Product", "widget": "text", "name": "__str__", "readonly": true}
]
}
drf-schema-adapter provides information about and ember-cli-crudities consumes: