how to serialize objects with specific fields using serializers.serialize in django?












-1















I want to serialize django models for only some specific fields. How do I do that. I have a model as below:



class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)

def __str__(self):
return self.first_name


I am using serailizer as:



from django.core import serializers

serializers.serialize('json', Person.objects.all(), content_type='application/json')


My output is:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello",
"last_name": "world"
}
}
]


I want to serialize this model only for first_name and output must be as below:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello"
}
}
]









share|improve this question




















  • 1





    To do what? If you just write Person.objects.all(), then you get the desirded queryset, so I do not really understand the problem. Note that Queryset.values() does not construct a dictionary, but a queryset of dictionaries.

    – Willem Van Onsem
    Nov 25 '18 at 10:36











  • I do not understand what <QuerySet [<Person: Hello>]> means? Hello is a string how to map it to Person object? and even it happened this is not the pattern of list. Question is totally wrong.

    – Rarblack
    Nov 25 '18 at 10:45











  • @WillemVanOnsem I just want to serialize the returned queryset from database using django.core.serializers.Serialize for only specific fields. so if i query my database using .values() i get queryset of dictionaries which raises 'dict' object has no attribute '_meta' but it works fine with queryset returned from .all()

    – dip bazz
    Nov 25 '18 at 10:55











  • @Rarblack <QuerySet [<Person: Hello>]> means I guess its a object. Hello is returned by str function from model though its an object representing a Person.

    – dip bazz
    Nov 25 '18 at 10:58






  • 1





    @dip bazz, if you want to use Django serializers, then ask question about that! Use field argument in serializer if you only want a subset of fields.

    – Borut
    Nov 25 '18 at 12:00
















-1















I want to serialize django models for only some specific fields. How do I do that. I have a model as below:



class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)

def __str__(self):
return self.first_name


I am using serailizer as:



from django.core import serializers

serializers.serialize('json', Person.objects.all(), content_type='application/json')


My output is:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello",
"last_name": "world"
}
}
]


I want to serialize this model only for first_name and output must be as below:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello"
}
}
]









share|improve this question




















  • 1





    To do what? If you just write Person.objects.all(), then you get the desirded queryset, so I do not really understand the problem. Note that Queryset.values() does not construct a dictionary, but a queryset of dictionaries.

    – Willem Van Onsem
    Nov 25 '18 at 10:36











  • I do not understand what <QuerySet [<Person: Hello>]> means? Hello is a string how to map it to Person object? and even it happened this is not the pattern of list. Question is totally wrong.

    – Rarblack
    Nov 25 '18 at 10:45











  • @WillemVanOnsem I just want to serialize the returned queryset from database using django.core.serializers.Serialize for only specific fields. so if i query my database using .values() i get queryset of dictionaries which raises 'dict' object has no attribute '_meta' but it works fine with queryset returned from .all()

    – dip bazz
    Nov 25 '18 at 10:55











  • @Rarblack <QuerySet [<Person: Hello>]> means I guess its a object. Hello is returned by str function from model though its an object representing a Person.

    – dip bazz
    Nov 25 '18 at 10:58






  • 1





    @dip bazz, if you want to use Django serializers, then ask question about that! Use field argument in serializer if you only want a subset of fields.

    – Borut
    Nov 25 '18 at 12:00














-1












-1








-1








I want to serialize django models for only some specific fields. How do I do that. I have a model as below:



class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)

def __str__(self):
return self.first_name


I am using serailizer as:



from django.core import serializers

serializers.serialize('json', Person.objects.all(), content_type='application/json')


My output is:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello",
"last_name": "world"
}
}
]


I want to serialize this model only for first_name and output must be as below:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello"
}
}
]









share|improve this question
















I want to serialize django models for only some specific fields. How do I do that. I have a model as below:



class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)

def __str__(self):
return self.first_name


I am using serailizer as:



from django.core import serializers

serializers.serialize('json', Person.objects.all(), content_type='application/json')


My output is:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello",
"last_name": "world"
}
}
]


I want to serialize this model only for first_name and output must be as below:



[
{
model: "myapp.Person",
pk: 1,
fields: {
"first_name": "hello"
}
}
]






python django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 7 '18 at 10:58







dip bazz

















asked Nov 25 '18 at 10:31









dip bazzdip bazz

459




459








  • 1





    To do what? If you just write Person.objects.all(), then you get the desirded queryset, so I do not really understand the problem. Note that Queryset.values() does not construct a dictionary, but a queryset of dictionaries.

    – Willem Van Onsem
    Nov 25 '18 at 10:36











  • I do not understand what <QuerySet [<Person: Hello>]> means? Hello is a string how to map it to Person object? and even it happened this is not the pattern of list. Question is totally wrong.

    – Rarblack
    Nov 25 '18 at 10:45











  • @WillemVanOnsem I just want to serialize the returned queryset from database using django.core.serializers.Serialize for only specific fields. so if i query my database using .values() i get queryset of dictionaries which raises 'dict' object has no attribute '_meta' but it works fine with queryset returned from .all()

    – dip bazz
    Nov 25 '18 at 10:55











  • @Rarblack <QuerySet [<Person: Hello>]> means I guess its a object. Hello is returned by str function from model though its an object representing a Person.

    – dip bazz
    Nov 25 '18 at 10:58






  • 1





    @dip bazz, if you want to use Django serializers, then ask question about that! Use field argument in serializer if you only want a subset of fields.

    – Borut
    Nov 25 '18 at 12:00














  • 1





    To do what? If you just write Person.objects.all(), then you get the desirded queryset, so I do not really understand the problem. Note that Queryset.values() does not construct a dictionary, but a queryset of dictionaries.

    – Willem Van Onsem
    Nov 25 '18 at 10:36











  • I do not understand what <QuerySet [<Person: Hello>]> means? Hello is a string how to map it to Person object? and even it happened this is not the pattern of list. Question is totally wrong.

    – Rarblack
    Nov 25 '18 at 10:45











  • @WillemVanOnsem I just want to serialize the returned queryset from database using django.core.serializers.Serialize for only specific fields. so if i query my database using .values() i get queryset of dictionaries which raises 'dict' object has no attribute '_meta' but it works fine with queryset returned from .all()

    – dip bazz
    Nov 25 '18 at 10:55











  • @Rarblack <QuerySet [<Person: Hello>]> means I guess its a object. Hello is returned by str function from model though its an object representing a Person.

    – dip bazz
    Nov 25 '18 at 10:58






  • 1





    @dip bazz, if you want to use Django serializers, then ask question about that! Use field argument in serializer if you only want a subset of fields.

    – Borut
    Nov 25 '18 at 12:00








1




1





To do what? If you just write Person.objects.all(), then you get the desirded queryset, so I do not really understand the problem. Note that Queryset.values() does not construct a dictionary, but a queryset of dictionaries.

– Willem Van Onsem
Nov 25 '18 at 10:36





To do what? If you just write Person.objects.all(), then you get the desirded queryset, so I do not really understand the problem. Note that Queryset.values() does not construct a dictionary, but a queryset of dictionaries.

– Willem Van Onsem
Nov 25 '18 at 10:36













I do not understand what <QuerySet [<Person: Hello>]> means? Hello is a string how to map it to Person object? and even it happened this is not the pattern of list. Question is totally wrong.

– Rarblack
Nov 25 '18 at 10:45





I do not understand what <QuerySet [<Person: Hello>]> means? Hello is a string how to map it to Person object? and even it happened this is not the pattern of list. Question is totally wrong.

– Rarblack
Nov 25 '18 at 10:45













@WillemVanOnsem I just want to serialize the returned queryset from database using django.core.serializers.Serialize for only specific fields. so if i query my database using .values() i get queryset of dictionaries which raises 'dict' object has no attribute '_meta' but it works fine with queryset returned from .all()

– dip bazz
Nov 25 '18 at 10:55





@WillemVanOnsem I just want to serialize the returned queryset from database using django.core.serializers.Serialize for only specific fields. so if i query my database using .values() i get queryset of dictionaries which raises 'dict' object has no attribute '_meta' but it works fine with queryset returned from .all()

– dip bazz
Nov 25 '18 at 10:55













@Rarblack <QuerySet [<Person: Hello>]> means I guess its a object. Hello is returned by str function from model though its an object representing a Person.

– dip bazz
Nov 25 '18 at 10:58





@Rarblack <QuerySet [<Person: Hello>]> means I guess its a object. Hello is returned by str function from model though its an object representing a Person.

– dip bazz
Nov 25 '18 at 10:58




1




1





@dip bazz, if you want to use Django serializers, then ask question about that! Use field argument in serializer if you only want a subset of fields.

– Borut
Nov 25 '18 at 12:00





@dip bazz, if you want to use Django serializers, then ask question about that! Use field argument in serializer if you only want a subset of fields.

– Borut
Nov 25 '18 at 12:00












1 Answer
1






active

oldest

votes


















0














Person.objects.all() also has the first_name value in it. You can access it via:



for p in Person.objects.all():
p.first_name


Please read the documentation for more details.



Update:



For serialization, try like this:



serializers.serialize("json", Person.objects.all(), fields=["first_name", "last_name"])





share|improve this answer


























  • Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

    – dip bazz
    Nov 25 '18 at 11:17











  • @dipbazz please see my updated answer.

    – ruddra
    Nov 25 '18 at 12:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466607%2fhow-to-serialize-objects-with-specific-fields-using-serializers-serialize-in-dja%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Person.objects.all() also has the first_name value in it. You can access it via:



for p in Person.objects.all():
p.first_name


Please read the documentation for more details.



Update:



For serialization, try like this:



serializers.serialize("json", Person.objects.all(), fields=["first_name", "last_name"])





share|improve this answer


























  • Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

    – dip bazz
    Nov 25 '18 at 11:17











  • @dipbazz please see my updated answer.

    – ruddra
    Nov 25 '18 at 12:03
















0














Person.objects.all() also has the first_name value in it. You can access it via:



for p in Person.objects.all():
p.first_name


Please read the documentation for more details.



Update:



For serialization, try like this:



serializers.serialize("json", Person.objects.all(), fields=["first_name", "last_name"])





share|improve this answer


























  • Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

    – dip bazz
    Nov 25 '18 at 11:17











  • @dipbazz please see my updated answer.

    – ruddra
    Nov 25 '18 at 12:03














0












0








0







Person.objects.all() also has the first_name value in it. You can access it via:



for p in Person.objects.all():
p.first_name


Please read the documentation for more details.



Update:



For serialization, try like this:



serializers.serialize("json", Person.objects.all(), fields=["first_name", "last_name"])





share|improve this answer















Person.objects.all() also has the first_name value in it. You can access it via:



for p in Person.objects.all():
p.first_name


Please read the documentation for more details.



Update:



For serialization, try like this:



serializers.serialize("json", Person.objects.all(), fields=["first_name", "last_name"])






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 12:01

























answered Nov 25 '18 at 10:35









ruddraruddra

13.3k32648




13.3k32648













  • Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

    – dip bazz
    Nov 25 '18 at 11:17











  • @dipbazz please see my updated answer.

    – ruddra
    Nov 25 '18 at 12:03



















  • Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

    – dip bazz
    Nov 25 '18 at 11:17











  • @dipbazz please see my updated answer.

    – ruddra
    Nov 25 '18 at 12:03

















Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

– dip bazz
Nov 25 '18 at 11:17





Actually I get a column name from somewhere like: columns = get_column_name(model=ModelName) and use it as: query = Person.objects.values(*columns) So I'm looking for a way to get back an object from the database as Person.objects.all() returns but Person.objects.values() return queryset of dictionaries.

– dip bazz
Nov 25 '18 at 11:17













@dipbazz please see my updated answer.

– ruddra
Nov 25 '18 at 12:03





@dipbazz please see my updated answer.

– ruddra
Nov 25 '18 at 12:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466607%2fhow-to-serialize-objects-with-specific-fields-using-serializers-serialize-in-dja%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks