python - How can I serialize single object's fields in django(without pk and model) -
In Django, I have this model (it has been obtained from the AbBabBase user):
Class User (Sarbbase User): UserName = Model.Carfild (Max_Lang = 20, Exclusive = True) RealName = Model.Carfild (max_long = 10) Grade = Model.Carfild (Max_Langu = 10) Student = Model .Carfild (max_long = 10) email = model. I want to serialize a single user object:"Admin", "grade": "lu", "username": "admin", "lu", "realname": "lu", "email": "admin", "grade" "is_active": true} / Code>
Is there a utility to serialize?
I have found the Dengue Form Cocument, it can only serialize a list and be with
model and
pk needed. It is like this:
[{"fields": {"email": "admin@admin.com", "is_active": true, "not student": "lu", " "Lu"}, "pk": 1, "user name": "account.user"}]
: "adminname", "realname": "lu", "grade" "Model"I also try the Build-in module, but I must get the key and value of every field, save it to the list, and it must be serial. It does not look elegant.
You can create custom
serializer in this way, django.core.serializers.json Import serializer, import django.utils from DjangoJSONEncoder to simple json class NewSerializer: serializer: def end_serialization (self): cleaned_objects = [] obj in for self.object: del obj ['pk' ] del obj ['model'] cleaned_objects.append (obj) simplejson.dump (cleaned_objects, self.stream, cls = DjangoJSONEncoder, ** auto.))
Comments
Post a Comment