Django access nested object field in serializer





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I Django-Rest have a class User that contain the field first_name, and a class Account that contain the fields username and a_class_ref that is a one-to-one' relation.



How it is possible in the serializer of B to do something like :



class AccountSerializer():
class Meta:
model= Account
fields= [
'username',
'firstname`
]


Account :



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)
def username(self):
return self.user.username <== this is the solution that I'm trying to avoid


And User is the extended AbstractUser from Django-rest-framework, that comes with a first_name = models.CharField(_('first name'), max_length=30, blank=True)



Thank you










share|improve this question

























  • you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation

    – Xepe
    Nov 16 '18 at 19:18











  • Yes but I'd like to do it a way that at the end, the json is not in "nested format", so it's transparent to the user

    – Boris Le Méec
    Nov 16 '18 at 19:23











  • May you add the model's code? Also if instead of using those cryptic names you used the real ones it would be easier to help you.

    – grouchoboy
    Nov 16 '18 at 19:52


















0















I Django-Rest have a class User that contain the field first_name, and a class Account that contain the fields username and a_class_ref that is a one-to-one' relation.



How it is possible in the serializer of B to do something like :



class AccountSerializer():
class Meta:
model= Account
fields= [
'username',
'firstname`
]


Account :



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)
def username(self):
return self.user.username <== this is the solution that I'm trying to avoid


And User is the extended AbstractUser from Django-rest-framework, that comes with a first_name = models.CharField(_('first name'), max_length=30, blank=True)



Thank you










share|improve this question

























  • you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation

    – Xepe
    Nov 16 '18 at 19:18











  • Yes but I'd like to do it a way that at the end, the json is not in "nested format", so it's transparent to the user

    – Boris Le Méec
    Nov 16 '18 at 19:23











  • May you add the model's code? Also if instead of using those cryptic names you used the real ones it would be easier to help you.

    – grouchoboy
    Nov 16 '18 at 19:52














0












0








0








I Django-Rest have a class User that contain the field first_name, and a class Account that contain the fields username and a_class_ref that is a one-to-one' relation.



How it is possible in the serializer of B to do something like :



class AccountSerializer():
class Meta:
model= Account
fields= [
'username',
'firstname`
]


Account :



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)
def username(self):
return self.user.username <== this is the solution that I'm trying to avoid


And User is the extended AbstractUser from Django-rest-framework, that comes with a first_name = models.CharField(_('first name'), max_length=30, blank=True)



Thank you










share|improve this question
















I Django-Rest have a class User that contain the field first_name, and a class Account that contain the fields username and a_class_ref that is a one-to-one' relation.



How it is possible in the serializer of B to do something like :



class AccountSerializer():
class Meta:
model= Account
fields= [
'username',
'firstname`
]


Account :



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)
def username(self):
return self.user.username <== this is the solution that I'm trying to avoid


And User is the extended AbstractUser from Django-rest-framework, that comes with a first_name = models.CharField(_('first name'), max_length=30, blank=True)



Thank you







python django django-rest-framework






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 20:01







Boris Le Méec

















asked Nov 16 '18 at 19:16









Boris Le MéecBoris Le Méec

767522




767522













  • you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation

    – Xepe
    Nov 16 '18 at 19:18











  • Yes but I'd like to do it a way that at the end, the json is not in "nested format", so it's transparent to the user

    – Boris Le Méec
    Nov 16 '18 at 19:23











  • May you add the model's code? Also if instead of using those cryptic names you used the real ones it would be easier to help you.

    – grouchoboy
    Nov 16 '18 at 19:52



















  • you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation

    – Xepe
    Nov 16 '18 at 19:18











  • Yes but I'd like to do it a way that at the end, the json is not in "nested format", so it's transparent to the user

    – Boris Le Méec
    Nov 16 '18 at 19:23











  • May you add the model's code? Also if instead of using those cryptic names you used the real ones it would be easier to help you.

    – grouchoboy
    Nov 16 '18 at 19:52

















you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation

– Xepe
Nov 16 '18 at 19:18





you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation

– Xepe
Nov 16 '18 at 19:18













Yes but I'd like to do it a way that at the end, the json is not in "nested format", so it's transparent to the user

– Boris Le Méec
Nov 16 '18 at 19:23





Yes but I'd like to do it a way that at the end, the json is not in "nested format", so it's transparent to the user

– Boris Le Méec
Nov 16 '18 at 19:23













May you add the model's code? Also if instead of using those cryptic names you used the real ones it would be easier to help you.

– grouchoboy
Nov 16 '18 at 19:52





May you add the model's code? Also if instead of using those cryptic names you used the real ones it would be easier to help you.

– grouchoboy
Nov 16 '18 at 19:52












1 Answer
1






active

oldest

votes


















1














You can declare a custom field with the source attribute:



class BSerializer(serializers.ModelSerializer):
a_field = serializers.CharField(source='a_class_ref.a_field')

class Meta:
model= B
fields= ['b_field', 'a_field']


Edit



Based on the models you posted, the following should work:



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)

class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
firstname = serializers.CharField(source='user.first_name')

class Meta:
model= Account
fields= ['username', 'firstname']





share|improve this answer


























  • It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

    – Boris Le Méec
    Nov 16 '18 at 19:27













  • I edited my original post, in B, A is a one-to-one relation field

    – Boris Le Méec
    Nov 16 '18 at 19:32











  • @BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

    – slider
    Nov 16 '18 at 19:34











  • I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

    – Boris Le Méec
    Nov 16 '18 at 19:38






  • 1





    That was probably a typo mistake, I just copy paste your code and it's working now thank you !

    – Boris Le Méec
    Nov 16 '18 at 20:41












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%2f53344075%2fdjango-access-nested-object-field-in-serializer%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









1














You can declare a custom field with the source attribute:



class BSerializer(serializers.ModelSerializer):
a_field = serializers.CharField(source='a_class_ref.a_field')

class Meta:
model= B
fields= ['b_field', 'a_field']


Edit



Based on the models you posted, the following should work:



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)

class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
firstname = serializers.CharField(source='user.first_name')

class Meta:
model= Account
fields= ['username', 'firstname']





share|improve this answer


























  • It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

    – Boris Le Méec
    Nov 16 '18 at 19:27













  • I edited my original post, in B, A is a one-to-one relation field

    – Boris Le Méec
    Nov 16 '18 at 19:32











  • @BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

    – slider
    Nov 16 '18 at 19:34











  • I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

    – Boris Le Méec
    Nov 16 '18 at 19:38






  • 1





    That was probably a typo mistake, I just copy paste your code and it's working now thank you !

    – Boris Le Méec
    Nov 16 '18 at 20:41
















1














You can declare a custom field with the source attribute:



class BSerializer(serializers.ModelSerializer):
a_field = serializers.CharField(source='a_class_ref.a_field')

class Meta:
model= B
fields= ['b_field', 'a_field']


Edit



Based on the models you posted, the following should work:



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)

class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
firstname = serializers.CharField(source='user.first_name')

class Meta:
model= Account
fields= ['username', 'firstname']





share|improve this answer


























  • It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

    – Boris Le Méec
    Nov 16 '18 at 19:27













  • I edited my original post, in B, A is a one-to-one relation field

    – Boris Le Méec
    Nov 16 '18 at 19:32











  • @BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

    – slider
    Nov 16 '18 at 19:34











  • I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

    – Boris Le Méec
    Nov 16 '18 at 19:38






  • 1





    That was probably a typo mistake, I just copy paste your code and it's working now thank you !

    – Boris Le Méec
    Nov 16 '18 at 20:41














1












1








1







You can declare a custom field with the source attribute:



class BSerializer(serializers.ModelSerializer):
a_field = serializers.CharField(source='a_class_ref.a_field')

class Meta:
model= B
fields= ['b_field', 'a_field']


Edit



Based on the models you posted, the following should work:



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)

class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
firstname = serializers.CharField(source='user.first_name')

class Meta:
model= Account
fields= ['username', 'firstname']





share|improve this answer















You can declare a custom field with the source attribute:



class BSerializer(serializers.ModelSerializer):
a_field = serializers.CharField(source='a_class_ref.a_field')

class Meta:
model= B
fields= ['b_field', 'a_field']


Edit



Based on the models you posted, the following should work:



class Account(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
related_name='account',
on_delete=models.CASCADE
)

class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
firstname = serializers.CharField(source='user.first_name')

class Meta:
model= Account
fields= ['username', 'firstname']






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 20:35

























answered Nov 16 '18 at 19:23









sliderslider

8,56811331




8,56811331













  • It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

    – Boris Le Méec
    Nov 16 '18 at 19:27













  • I edited my original post, in B, A is a one-to-one relation field

    – Boris Le Méec
    Nov 16 '18 at 19:32











  • @BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

    – slider
    Nov 16 '18 at 19:34











  • I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

    – Boris Le Méec
    Nov 16 '18 at 19:38






  • 1





    That was probably a typo mistake, I just copy paste your code and it's working now thank you !

    – Boris Le Méec
    Nov 16 '18 at 20:41



















  • It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

    – Boris Le Méec
    Nov 16 '18 at 19:27













  • I edited my original post, in B, A is a one-to-one relation field

    – Boris Le Méec
    Nov 16 '18 at 19:32











  • @BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

    – slider
    Nov 16 '18 at 19:34











  • I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

    – Boris Le Méec
    Nov 16 '18 at 19:38






  • 1





    That was probably a typo mistake, I just copy paste your code and it's working now thank you !

    – Boris Le Méec
    Nov 16 '18 at 20:41

















It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

– Boris Le Méec
Nov 16 '18 at 19:27







It sounds like it is exactly what I was looking for, but I still have this exception : django.core.exceptions.ImproperlyConfigured: Field name firstname is not valid for model Account. (Account is B, and firstname is a_field)

– Boris Le Méec
Nov 16 '18 at 19:27















I edited my original post, in B, A is a one-to-one relation field

– Boris Le Méec
Nov 16 '18 at 19:32





I edited my original post, in B, A is a one-to-one relation field

– Boris Le Méec
Nov 16 '18 at 19:32













@BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

– slider
Nov 16 '18 at 19:34





@BorisLeMéec you can't directly access a_fileld. Note that I'm going via a_class_ref. What is the field name on Account that you use to reference the other model?

– slider
Nov 16 '18 at 19:34













I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

– Boris Le Méec
Nov 16 '18 at 19:38





I wrote firstname = serializers.CharField(source='user.firstname') and user is the one-to-one related field in the B class, that refers to the A class

– Boris Le Méec
Nov 16 '18 at 19:38




1




1





That was probably a typo mistake, I just copy paste your code and it's working now thank you !

– Boris Le Méec
Nov 16 '18 at 20:41





That was probably a typo mistake, I just copy paste your code and it's working now thank you !

– Boris Le Méec
Nov 16 '18 at 20:41




















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%2f53344075%2fdjango-access-nested-object-field-in-serializer%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

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma