AncientSwordRage
AncientSwordRage

Reputation: 7608

Read only field from related model not appearing, with Django Rest Framework?

I have two models (and an abstract model): User, Mage and Character. Mage 'inherits' from Character.

Character has an updated_date field, and I'm trying to pull through the latest date that a User's Mage was updated:

class UserSerializer(serializers.ModelSerializer):
    mage_by_user = serializers.PrimaryKeyRelatedField(
        many=True, queryset=Mage.objects.all())
    mage_last_updated = serializers.ReadOnlyField(
        source='mage_by_user.updated_date')

    class Meta:
        model = User
        fields = ('id', 'username', 'mage_by_user', 'mage_last_updated',)

But for some reason the 'mage_last_updated' field just does not appear! If I delete 'mage_by_user' then it errors, complaining that the 'mage_last_updated' field needs it to exist. So somewhere it recognises it.

How do I troubleshoot this? How do I fix it?


Edit:

Here is the json with a few edits, for the sake of this question (namely, I've inserted the mage object, to show the updated_date field)

{
    "id":1,
    "username":"admin",
    "mage_by_user":[{
        "id":3,
        "player":"admin",
        "name":"gandalf",
        "sub_race":"Foo",
        "faction":"Foo",
        "is_published":false,
        "updated_date":"2015-02-11T16:13:16.229890Z",
        "power_level":1,
        "energy_trait":7,
        "virtue":"prudence",
        "vice":"lust",
        "morality":7,
        "size":5,
        "arcana":{
            "Matter":0,
            "Death":0,
            "Fate":1,
            "Mind":0,
            "Prime":0,
            "Forces":0,
            "Spirit":0,
            "Time":0,
            "Space":0,
            "Life":0
        },            
        "mental_attributes":{
            "Wits":0,
            "Intelligence":1,
            "Resolve":0
        },
        "physical_attributes":{
            "Dexterity":0,
            "Stamina":0,
            "Strength":0
        },
        social_attributes":{
            "Composure":0,
            "Presence":0,
            "Manipulation":0
        },
        "mental_skills":{
            "Politics":0,
            "Academics":1,
            "Crafts":0,
            "Medicine":0,
            "Occult":0,
            "Science":0,
            "Investigation":0,
            "Computer":0
        },
        "physical_skills":{
            "Survival":0,
            "Athletics":0,
            "Weaponry":0,
            "Stealth":0,
            "Larceny":0,
            "Brawl":0,
            "Drive":0,
            "Firearms":0
        },
        "social_skills":{
            "Subterfuge":0,
            "Expression":0,
            "Socialize":0,
            "Streetwise":0,
            "Animal Ken":0,
            "Persuasion":0,
            "Empathy":0,
            "Intimidation":0
        }
    }]
}

Also, adding many=True is not correct for the read only field mage_last_updated, as Django complains. Here mage_by_user is the list of mages 'owned' by the user.

Upvotes: 1

Views: 2017

Answers (1)

Sam R.
Sam R.

Reputation: 16450

Based on discussion we had in the chat:

class UserSerializer(serializers.ModelSerializer):
    mage_by_user = serializers.PrimaryKeyRelatedField(
        many=True, queryset=Mage.objects.all())
    mage_last_updated = serializers.SerializerMethodField()

    class Meta:
        model = User
        fields = ('id', 'username', 'mage_by_user', 'mage_last_updated',)

    def get_mage_last_updated(self, obj):
        latest_mage = obj.mage_set.latest('updated_date')
        return latest_mage.updated_date

Upvotes: 1

Related Questions