1

In my Django apllication, I need to take fields from two different tables (models.py classes), lets say username form one table and other information from some other table. this fields will be in the from of list since I use values_list to retrieve them. But the thing is I need to concate this two list and use serializer mechanism so that further I can prepare a json out of it. The Django's default serializer does not support this. Is there any other alternative for django serilizer that supports different types of objects in a list? sample code:

Fields from projectrunlog table in models.py

user_project_list = projectrunlog_object.values_list('user_key','project_run_date','project_run_status') 

Fields from user info table in models.py

user_name = User_obj.values_list('user_name')

Not sure, but want to try something like this and make a json out of field and fields2

fields = serializers.serialize('json', list(projectrunlog_object), fields = ('user_key','project_run_date','project_run_status'))
fields2 = serializers.serialize('json', list(User_obj), fields = ('user_name'))

So, in my json, I want user_name as well along with other four fields, and should work seamlessly, without knowing that user_name comes from different table. kindly suggest what to do.

As suggested by GwynBleidD, I cannot do :json.dumps(list(user_project_list)) Because here, user key is actually a UUID field. It is showing error as "UUId is not JSON serializable". I am using Django 1.8 wherein UUID implementation is default. Any solution??

1
  • can you kindly format (markdown) your code so we can read it better? Commented Oct 14, 2015 at 7:45

1 Answer 1

2

Serializer is for serializing django objects, right now you have list of tuples instead, so django serializer won't handle that.

If you want to connect this 2 lists together, you must get not only user_name from your User_obj, but also user_key (id?) for connecting it with second model. If user_key is actually an ForeignKey, you can do this:

user_project_list = projectrunlog_object.values_list('user_key__user_name','project_run_date','project_run_status')

If not, you must handle it by yourself in python.

After creating list of dicts or tuples, you don't have to run django serializer, normal json serializer will do the job:

json.dumps(list(user_project_list))
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for answering.. Doing manually by ourselves is the option, I agree..But is there any serializer implemented aprt from the one that comes with django (that should support multple kinds of instances of course)? There is one such implemntation called as serpy, but i am not sure wheathet it fulfills my requirement.
There are many serializers provided, for example serializers built into Django REST framework, but you're dealing in that exact example with standard python data types. That can be serialized using json library and there is no need for any complex logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.