Last active
August 29, 2015 14:04
-
-
Save niktto/139980988f627339d58b to your computer and use it in GitHub Desktop.
Sample code for blog post "Write more classes"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Account | |
fields = ('id', 'account_name', 'user_set', 'created') | |
depth = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomUserSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = User | |
fields = ('id', 'realname') | |
class AccountSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Account | |
fields = ('id', 'account_name', 'user_set', 'created') | |
depth = 1 | |
def get_nested_field(self, model_field, related_model, to_many): | |
if related_model == User: | |
return CustomUserSerializer(many=to_many) | |
else: | |
return super(AccountSerializer, self).get_nested_field( | |
model_field, related_model, to_many | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment