I forgot to show the stacktrace:
Internal Server Error: /products/
Traceback (most recent call last):
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/core/handlers/exception.py",
line 35, in inner
response = get_response(request)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/core/handlers/base.py",
line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/core/handlers/base.py",
line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/views/decorators/csrf.py",
line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/soulasb/projects/POC/PocEms/Api/views.py", line 25, in
ProductList
wSerializer.save()
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/rest_framework/serializers.py",
line 214, in save
self.instance = self.create(validated_data)
File "/home/soulasb/projects/POC/PocEms/Api/serializers.py", line 26, in
create
wProduct = Product.objects.create(**validated_data)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/manager.py",
line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/query.py",
line 417, in create
obj.save(force_insert=True, using=self.db)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/base.py",
line 729, in save
force_update=force_update, update_fields=update_fields)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/base.py",
line 759, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using,
update_fields)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/base.py",
line 842, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk,
raw)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/base.py",
line 880, in _do_insert
using=using, raw=raw)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/manager.py",
line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/query.py",
line 1125, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 1284, in execute_sql
for sql, params in self.as_sql():
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 1237, in as_sql
for obj in self.query.objs
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 1237, in <listcomp>
for obj in self.query.objs
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 1236, in <listcomp>
[self.prepare_value(field, self.pre_save_val(field, obj)) for field in
fields]
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/sql/compiler.py",
line 1176, in prepare_value
value = field.get_db_prep_save(value, connection=self.connection)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/django/db/models/fields/__init__.py",
line 767, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection,
prepared=False)
File
"/home/soulasb/dev-tmp/POC/EmsDRFDjongo/venv-app/lib/python3.6/site-packages/djongo/models/fields.py",
line 461, in get_db_prep_value
model=Model
ValueError: Value: None must be instance of Model: <class
'django.db.models.base.Model'>
Post by Benjamin SOULASHi,
Well indeed in my first version I used django models, and the reason why
it worked with simple attributes I think it is because the were simple and
because the engine was defined into the settings, I dont know how but it
did the job.
Now I changed my code to introduce EmbeddedField, But it does not work.
Just in case, I use a Postman client to test my API, and I am wondering if
models.py
from djongo import models
sourceId = models.IntegerField(default=-1)
usageId = models.IntegerField(default=0)
channelId = models.IntegerField(default=0)
cabinetId = models.IntegerField(default=0)
zoneId = models.IntegerField(default=0)
dateCreation = models.DateTimeField(auto_now=True)
dateUpdate = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=50, default="Unknown product name")
channels = models.EmbeddedModelField(
model_container=Channel,
)
views.py
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from Api.models import Product, Channel
from Api.serializers import ProductSerializer, ChannelSerializer
@csrf_exempt
"""
@brief List all products, or create a new product.
"""
wProducts = Product.objects.all()
wSerializer = ProductSerializer(wProducts, many=True)
return JsonResponse(wSerializer.data, safe=False)
data = JSONParser().parse(aRequest)
wSerializer = ProductSerializer(data=data)
wSerializer.save()
return JsonResponse(wSerializer.data, status=201)
return JsonResponse(wSerializer.errors, status=400)
@csrf_exempt
"""
@brief Retrieve, update or delete a product.
"""
wProducts = Product.objects.get(pk=pk)
return HttpResponse(status=404)
wSerializer = ProductSerializer(wProducts)
return JsonResponse(wSerializer.data)
data = JSONParser().parse(aRequest)
wSerializer = ProductSerializer(wProducts, data=data)
wSerializer.save()
return JsonResponse(wSerializer.data)
return JsonResponse(wSerializer.errors, status=400)
Product.delete()
return HttpResponse(status=204)
@csrf_exempt
"""
@brief List all products, or create a new channel.
"""
wChannels = Channel.objects.all()
wSerializer = ChannelSerializer(wChannels, many=True)
return JsonResponse(wSerializer.data, safe=False)
data = JSONParser().parse(aRequest)
wSerializer = ChannelSerializer(data=data)
wSerializer.save()
return JsonResponse(wSerializer.data, status=201)
return JsonResponse(wSerializer.errors, status=400)
@csrf_exempt
"""
@brief Retrieve, update or delete a channel.
"""
wChannel = Channel.objects.get(pk=pk)
return HttpResponse(status=404)
wSerializer = ChannelSerializer(wChannel)
return JsonResponse(wSerializer.data)
data = JSONParser().parse(aRequest)
wSerializer = ChannelSerializer(wChannel, data=data)
wSerializer.save()
return JsonResponse(wSerializer.data)
return JsonResponse(wSerializer.errors, status=400)
Channel.delete()
return HttpResponse(status=204)
serializers.py
from rest_framework import serializers
from Api.models import Product, Channel
model = Channel
fields = ('sourceId', 'usageId', 'channelId', 'cabinetId', 'zoneId')
channels = ChannelSerializer(many=True)
model = Product
fields = ('dateCreation', 'dateUpdate', 'name', 'channels')
wChannels = validated_data.pop("channels")
wProduct = Product.objects.create(**validated_data)
Channel.objects.create(product=wProduct, **wChannel)
return wProduct
Just to clarify, we agree that in order to send infos of the creation/get
of a product or product list, I have to implement DRF views and serializers
right?
I thought workflow was: -->JSON DATA --> DRF API --> DRF Views/serializers
--> Djongo Models --> Pymongo commands --> MongoDB
Post by Sagar BhadarkaHi Benjamin,
I went through your code. In the first model where you have only 1 model
which is product. over there you import model from django, so how did you
store data in mongodb. because if you want to store data in mongodb using
djongo then you have to import model from djongo and that you are doing in
the last block of code where you have 2 models channels and product.
In the second model where you defining that channels class, I think
instead of that many to many field may be you can use Embeded field. Please
refer to this link. I hope it is helpful.
https://github.com/nesdis/djongo/blob/master/docs/docs/get-started.md
Post by Benjamin SOULASHi Sagar,
Ok let's try this. So I have a model named *Product* in which I got
several attributes (eg: 3 simple like IntegerField or CharField), but I
also want to add an attribute "channel" that represents a list of
*Channel* objects.
First of all, I implemented DRF with *djongo* as database Engine with
models.py
from django.db import models
dateCreation = models.DateTimeField(auto_now=True)
dateUpdate = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=50, default="Unknown product name")
serializers.py
from rest_framework import serializers
from Api.models import Product, Channel
model = Product
fields = ('dateCreation', 'dateUpdate', 'name')
views.py
from django.http import HttpResponse, JsonResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from Api.models import Product
from Api.serializers import ProductSerializer
@csrf_exempt
"""
@brief List all products, or create a new product.
"""
wProducts = Product.objects.all()
wSerializer = ProductSerializer(wProducts, many=True)
return JsonResponse(wSerializer.data, safe=False)
data = JSONParser().parse(aRequest)
wSerializer = ProductSerializer(data=data)
wSerializer.save()
return JsonResponse(wSerializer.data, status=201)
return JsonResponse(wSerializer.errors, status=400)
@csrf_exempt
"""
@brief Retrieve, update or delete a product.
"""
wProducts = Product.objects.get(pk=pk)
return HttpResponse(status=404)
wSerializer = ProductSerializer(wProducts)
return JsonResponse(wSerializer.data)
data = JSONParser().parse(aRequest)
wSerializer = ProductSerializer(wProducts, data=data)
wSerializer.save()
return JsonResponse(wSerializer.data)
return JsonResponse(wSerializer.errors, status=400)
Product.delete()
return HttpResponse(status=204)
urls.py
from django.conf.urls import url
from Api import views
urlpatterns = [
url(r'^products/$', views.ProductList),
url(r'^products/(?P<pk>[0-9]+)/$', views.ProductDetail)
]
But now my main problem is to know how to integrate an object as an
attribute, and I watched several examples and now from *nsdis djongo
doc* I finally got this (I thought I had to use *djongo models* for
from djongo import models
sourceId = models.IntegerField(default=-1)
usageId = models.IntegerField(default=0)
channelId = models.IntegerField(default=0)
cabinetId = models.IntegerField(default=0)
zoneId = models.IntegerField(default=0)
dateCreation = models.DateTimeField(auto_now=True)
dateUpdate = models.DateTimeField(auto_now=True)
name = models.CharField(max_length=50, default="Unknown product name")
channels = models.ManyToManyField(Channel)
To be honest, I imagine I can do the same in Django Rest Framework
models, but also in DjOngo models, so I need advices to continue my app.
I Hope It will be clearer now
Kind regards
Post by Sagar BhadarkaI think what are you trying to say that you want to create embeded
document. Something like relation mapping that you do in SQL database.
Correct me If I am wrong. If you can give me a simple example for what are
you want to do then I can help you?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.