On Sat, 5 Aug 2023, at 06:47, Ryan Gartin wrote: > You are correct state_id is not a field on the model, state is, and using > “state” in the field lists works. > > I would like to propose that if ‘state_id’ is a valid property on the > queryset (and in the database) that is should be serialized.
I think the main limitation you're hitting here is the core serializers in Django are really primarily only tuned for generating fixtures; This is why pretty much every REST API framework for Django has had its own version of the idea. The built in ones are simply not flexible enough. IMHO the serializer is the most important part of any REST / API framework. > While I understand that this is my personal preference I think it is > reasonable to request that if QuerySet.state_id returns a valid value then it > should also be capable of being serialized as well. Way back in the mid 2000s I did my own sub-classes of the core Serializers to allow this sort of thing. Since then I've tried maybe half a dozen REST API libs (including writing my own), and none of them used core serializers. That's not to say your idea is without merit. I think if you give it an afternoon's digging you could add an option to the django core serializers to list non-field attributes to include. -- Curtis > > On Thursday, August 3, 2023 at 9:46:27 PM UTC-4 Curtis Maloney wrote: >> __ >> Hi Mike, >> >> On Fri, 4 Aug 2023, at 06:03, Ryan Gartin wrote: >>> I came across this issue calling the following and FK fields with _id are >>> ignored: >>> serialize('json', ...., fields=['title', 'state_id']). >> >> I think the problem you've run into here is "state_id" is not a Field on >> your model; it's where the raw PK value for your ForeignKey is stored. >> >> I've just double checked the code for serialize, and I'm pretty sure the >> default behavior for a ForeignKey field _is_ _already_ to output the PK >> value. >> >> What happens in your code if you put "state" instead of "state_id" in the >> field list? >> >> -- >> Curtis -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/d9e79eb8-aeea-48b1-9837-7e9761ffff4e%40app.fastmail.com.