How to restrict the list of fields in Django?

Trying to hook up like the simplest thing, but I can't.

There is a UserProfile Model with a bunch of fields and a serializer for this model (DRM) where all fields are listed.

Get the entry pass in the serializer and output to the console - it works. Shows ALL the fields.

I want to display only opredelennye field and then ambush.

Found that can be limited by using only and defer but they refuse to work. I can still see in the console ALL the fields.

up = UserProfile.objects.only('website', 'phone').get(user_id=2)

Expect the city (in the fourth line should be blank). But it's like all the other fields with their present values.

Something Google does not help me with the answer.
June 10th 19 at 15:53
2 answers
June 10th 19 at 15:55
Whenever you call only() it replaces the set of fields to load immediately. The method''s name is mnemonic: only those fields are loaded immediately; the remainder are deferred.

briefly, it works as you'd expect, but as soon as you have applied to a field in which not only is dosepro and it is loaded from the database.
And how then to proceed in my case? Have a serializer and I need to somewhere he was showing all the fields, and somewhere a certain set of fields. Something you do not want to write the same serializers with different sets of fields. - benton87 commented on June 10th 19 at 15:58
in the serializer, you can specify the desired fields. And so, you have such a large number of fields of the model are that the queries give different limit speed or is it premature optimization? - Garnett_Deckow commented on June 10th 19 at 16:01
Yes , the fact of the matter is that in the serializer, you can specify the desired fields. But what if one place needed one set of fields in another a different set. Copy-paste Serializers with different sets of fields? Not yet optimized and attempt snapit usual logic from another platform. I just Django sucks, but don't understand why such a big problem to serialize the object to json. I don't know is normal or not in python, but I would like to see here is a simple behavior (such as Salesforce/use a clone of java)
in the SQL query got 3 fields
Contact c = SELECT Id, Name, Birthdate FROM Contact WHERE Id = someId
String jsonResponse = JSON.serialize(c)
All! Get {"Id":"someId", "Name":"Pupkin", "Birthdate":"1983-10-10"}
Why in Django so complicated? Some serializers to write the fields to specify.
Maybe I missed something and not look back? Because next question I skzala that need to be serialized in JSON to use DRF. He may not need? - benton87 commented on June 10th 19 at 16:04
Well there is a difference, you used the sql queries used now ORM. There you have the fields specified in the sql query, now in the serializer. And Yes, the DRF is when you need a rest api and you don't want their bikes to pee.
I just Django sucks, but don't understand why such a big problem to serialize an object in json

I do not see any problem. - Garnett_Deckow commented on June 10th 19 at 16:07
, it is possible to draw an analogy Serializer == SQL query? If so, then like the picture. I thought that the Serializer == Model. Well there is a User model. So from it we get UserSerializator and already working with him. And it turns out that there is a User model is N serializers?
and everyone's already in charge of which fields get well, and other conditions.

But if I don't need no DRM and API. And just need to serialize the json that is pulled from a database in Django is it possible to implement on another?
Saw serializer in django.core but for some reason it only works with arrays. Just take one dances with a tambourine not frequency in JSON.

If you know just list all possible options, I will continue to develop. And then something like a simple question, but was delayed. - benton87 commented on June 10th 19 at 16:10
Can we get a list of fields not hardcoding a parameter to pass to the serializer? - benton87 commented on June 10th 19 at 16:13
Yes more like that you've got a mess in my head. What is DRM, it may still ORM or something else? And the serializer is, roughly speaking, the Converter in any portable format, for example, to pythonский list or dict to translate into json, in principle, no serializer not needed, but the datetime is already required. For simple cases in without DRF:
JsonResponse({'values': list(Model.objects.filter(conditions).values('id', 'name'))})
- Garnett_Deckow commented on June 10th 19 at 16:16
so if the field list is a parameter to the serializer, it's over they can be parameters to pass. - Garnett_Deckow commented on June 10th 19 at 16:19
I though I understood that here about the different serializers say Jankowski from DRF - Garnett_Deckow commented on June 10th 19 at 16:22
Thank you for your patience. Basically you have confirmed my fears. Something I mixed up :))) will be zero at the docks to read the Django Rest Framework. I hope understanding will come. On account of the simple version with JsonResponse - also try. Maybe it's just what I need. - benton87 commented on June 10th 19 at 16:25
Can cast the result as answer so I can mark as a Solution? You plus the karma will be. - benton87 commented on June 10th 19 at 16:28
And sorry!!!! Already marked as a solution :))) Thu propulse! - benton87 commented on June 10th 19 at 16:31
June 10th 19 at 15:57
Here is the solution I was looking for

from rest_flex_fields import FlexFieldsModelSerializer

class CountrySerializer(FlexFieldsModelSerializer):
 class Meta:
 model = Country
 fields = ['id', 'name', 'population']

class PersonSerializer(FlexFieldsModelSerializer):
 country: CountrySerializer(fields=['name'])
 class Meta:
 model = Person
 fields = ['id', 'name', 'country', 'occupation', 'hobbies']

serializer = PersonSerializer(person, fields=["id", "name", ""])

 "id" : 13322
 "name" : "John Doe",
 "country" : {
 "name" : "United States",

Find more questions by tags Django