4

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

3
  • you will need to create a serializer in order to display the information of a_class_ref, looks for nested serializer in the documentation Commented Nov 16, 2018 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 Commented Nov 16, 2018 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. Commented Nov 16, 2018 at 19:52

1 Answer 1

7

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']
Sign up to request clarification or add additional context in comments.

9 Comments

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)
I edited my original post, in B, A is a one-to-one relation field
@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?
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
That was probably a typo mistake, I just copy paste your code and it's working now thank you !
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.