Source field seems to be ignored on ModelField fields #9440
Replies: 2 comments 2 replies
-
I've never defined serializer fields like this before class UserBusinessTaskResultSerializer(serializers.ModelSerializer):
task_name = serializers.CharField(read_only=True, source='task.task_name')
class Meta:
model = UserBusinessTaskResult
fields = [
'user_id',
'task_name',
] Have you tried this? I'm assuming |
Beta Was this translation helpful? Give feedback.
-
Yeah, that does generally work, I just dislike the duplication of information about what type of data is going though here. It's a bit tedious in this case to go through and look up the definition and then map that to an appropriate DRF field when that mapping work has already been done. I'm not 100% sure on this, but I think it also looses (or you must copy over) various length and other restrictions too whereas just referencing the source DB field lets you pass all that data along. Finally, it looses or requires duplication of help text or other "metadata" type things that may be on the source field. But stepping back from the specifics of my usecase here from a logical standpoint my brain says "this DRF field is actually just a front for this DB field" and I wish there was an easy way to represent that (fully) in code. Also, if I'm reading the documentation what I have above should work (even if it wasn't how things were intended) but as far as I can tell from reading through the relevant DRF field, the |
Beta Was this translation helpful? Give feedback.
-
Hello!
I'll preface this with it's entirely possible I'm missing something, however, I can't see to get ModelField to work with specifying a
source
argument. From my reading of the docs, it seems like this should be supported but I've run in to many errors trying to make it work.models.py:
serializers.py:
Desired output from serializer:
Current error:
This tells me that the ModelField objects are ignoring the
source
argument I'm passing in to know where to source their data from (i.e. they're looking for it on the UserBusinessTaskResult object instead of the related TaskResult object).At a higher level, the problem I have is I have a DB model (TaskResult) that is coming in from a library that I cannot edit but I have "extended" it with my own custom table that has a FK to the library table. I originally had this as just a nested-sterilizer which is fine, but as a user of the API, it is slightly weird to have to go into a separate dictionary to access some fields about the task but not all. I am aware this can be done with SerializerMethodFields as well, however that seems fairly inefficient and like a lot of boiler plate for something that feels like it should be easy/common-ish.
What I would really love is just a "RelatedModelField" or something like that that would let me do this:
I feel like this is a fairly common pattern I've run into a few times while using DRF and never come up with a great way to resolve it. Is there a feature somewhere that makes this easy that I've missed?
Beta Was this translation helpful? Give feedback.
All reactions