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.
It is my preference that a serialized JSON property (to be consumed in a
strongly typed language) is reserved for a specific type. I realize Django
will determine that `state: int` is an Int and it needs to do a FK lookup
and populate that property with the state model but a strongly typed
language does not have that ability to determine between two types nor does
it have the ability to requery. So my preference when I consume JSON
response into my apps is to reserve state: { title: str, etc...} to an
object representing State model and use state_id: int wherever I just need
the Integer.
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.
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/a88b978f-0d9f-4ac9-a932-41de251dba64n%40googlegroups.com.