18👍
I ended up working around this by ‘manually’ achieving what I wanted using the underlying simplejson
serializer:
from django.utils import simplejson
json = simplejson.dumps( [{'name': o.name,
'country': o.country} for o in objects] )
A little more typing, but works.
13👍
What Josh did but backwards:
data = json.loads(json_string)
for d in data:
del d['pk']
del d['model']
data = json.dumps(data)
this way you don’t have to worry about updating the code when adding more fields in the future.
- [Django]-Django models avoid duplicates
- [Django]-Django character set with MySQL weirdness
- [Django]-Nginx doesn't serve static
9👍
You may also override JSON serializer as explained here: Override Django Object Serializer to get rid of specified model
from django.core.serializers.json import Serializer, DjangoJSONEncoder
from django.utils import simplejson
import logging
class MySerializer(Serializer):
def end_serialization(self):
cleaned_objects = []
for obj in self.objects:
del obj['pk']
cleaned_objects.append(obj)
simplejson.dump(cleaned_objects, self.stream, cls=DjangoJSONEncoder, **self.options)
- [Django]-How to test auto_now_add in django
- [Django]-Django Generic Views using decorator login_required
- [Django]-Django – getting Error "Reverse for 'detail' with no arguments not found. 1 pattern(s) tried:" when using {% url "music:fav" %}
3👍
Although this is an old question, someone else will probably come up with it on a Google search.
Unfortunately the django serializer offers fairly little customization like what you defined. My solution, if you know you will be using a lot of serialization for your project, was simply to copy django’s serialization stuff over to my own project and make some small changes. This isn’t ideal, but it does the job. Specifically, to remove the pk’s, there is a line in start_object(self, obj):
self.xml.startElement("object", {
"pk" : smart_unicode(obj._get_pk_val()),
"model" : smart_unicode(obj._meta),
})
Removing the “pk” line should fix it. It’s a somewhat dirty hack, since if they improve this later on it may require some changes to your views, but for me this was the easiest way around the limitations.
Hope this helps someone.
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
- [Django]-Exclude fields in Django admin for users other than superuser
- [Django]-How to format time in django-rest-framework's serializer?
3👍
I had the same problem, so I created my own Serializer
, inheritance from Serializer from Django.
I’d like only field’s data, so I overwrite the method get_dump_object
, and added the PK field.
from django.core.serializers.json import Serializer
class JSONSerializer(Serializer):
def get_dump_object(self, obj):
self._current[obj._meta.pk.name] = obj._get_pk_val()
return self._current
And call:
output = JSONSerializer().serialize(queryset)
- [Django]-Access web server on VirtualBox/Vagrant machine from host browser?
- [Django]-Warning: Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
-1👍
The ugly (but working) way :
data_tmp = data.split('{')
#Gets all the data after fields
response = "[{"+data_tmp[2].replace("}}","}",1)
- [Django]-Django 2 – How to register a user using email confirmation and CBVs?
- [Django]-Django Admin app or roll my own?
- [Django]-Django Footer and header on each page with {% extends }
-3👍
We don’t waste our time trying to “sanitize” the PK’s. When we produce a JSON record, the PK is there, and the user can ignore it if they want. They can’t do anything with the information, so it’s just clutter.
None of our web services interfaces allow anyone to provide a PK back to us. We do POST, PUT and DELETE searches on other fields, but not the PK.
The HTML, however, shows the PK’s the URL’s all the time. It lets people bookmark the pages.
- [Django]-Django-nonrel + Django-registration problem: unexpected keyword argument 'uidb36' when resetting password
- [Django]-Exclude fields in Django admin for users other than superuser
- [Django]-Name '_' is not defined