Django

Code

Ticket #2843 (new)

Opened 2 years ago

Last modified 1 year ago

[patch] serializer can't resolve a one-to-many relation from the "foreign" side

Reported by: nikl@nikl.net Assigned to: nobody
Milestone: Component: Core framework
Version: Keywords: serialize json python xml foreign key one to many relationship
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 1 Patch needs improvement: 1

Description

best illustrated with a short example:

class Reporter(models.Model):
    name = models.CharField(maxlength=25)

class Article(models.Model):
    reporter = models.ForeignKey(Reporter)

what I want to be able to get is something like the following (python-serialization):

[{'fields': {'articles': [1, 2, 3],
             'name': 'Me'},
  'model': 'news.reporter',
  'pk': '1'}]

currently, if I want to get the information in this format, I have two options:

  • construct a Model-like structure, so it can be used with the serializers
  • turn the one-to-many into a many-to-many relationship

both of these are obviously not a clean solution...

therefore I'd propose to add an extra parameter to export the ids of those related-objects optionally.

if time allows and there are no objections to this, I'll try to throw together a patch this weekend.

Attachments

svn.diff (3.4 kB) - added by "Nikolaus Schlemm" <nikl@nikl.net> on 10/04/06 06:23:54.
no time on the weekend, but now it's done :) one small change to what I previously wrote: using "article_set" instead of "articles" seemed much more consistent with the rest of the django api
svn.2.diff (3.7 kB) - added by "Nikolaus Schlemm" <nikl@nikl.net> on 10/04/06 07:46:50.
now with the paths relative to django root-dir, not the serializers dir
include_reverse_relations.diff (2.9 kB) - added by Nikolaus Schlemm <nikl@nikl.net> on 01/31/07 01:53:16.
just a quick option rename. unfortunately, I don't much more time right as I'm in the middle of starting a new job - plus moving from one city to another…
include_reverse_relations.2.diff (3.8 kB) - added by Nikolaus Schlemm <nikl@nikl.net> on 01/31/07 02:01:45.
argh! forgot django/core/serializers/python.py in the last diff…

Change History

10/04/06 06:23:54 changed by "Nikolaus Schlemm" <nikl@nikl.net>

  • attachment svn.diff added.

no time on the weekend, but now it's done :)

one small change to what I previously wrote: using "article_set" instead of "articles" seemed much more consistent with the rest of the django api

10/04/06 06:25:45 changed by "Nikolaus Schlemm" <nikl@nikl.net>

  • summary changed from serializer can't resolve a one-to-many relation from the "foreign" side to [patch] serializer can't resolve a one-to-many relation from the "foreign" side.

10/04/06 07:46:50 changed by "Nikolaus Schlemm" <nikl@nikl.net>

  • attachment svn.2.diff added.

now with the paths relative to django root-dir, not the serializers dir

11/28/06 18:36:53 changed by Nikolaus Schlemm <nikl@nikl.net>

  • type changed from defect to enhancement.
  • severity changed from major to normal.

01/17/07 16:12:17 changed by

  • milestone deleted.

Milestone Version 1.0 deleted

01/30/07 16:41:55 changed by jacob

  • needs_better_patch set to 1.
  • stage changed from Unreviewed to Accepted.
  • needs_tests set to 1.
  • needs_docs set to 1.

This looks good to me, but needs a few changes:

  • I'd like the option "export_o2m" to be changed to something more clear. How about "include_reverse_relations"?
  • Tests need to be written for the new behavior.
  • As does documentation, but since the serializers doc is not where close to finished and that's my fault, I'll check this in without docs if someone else can handle the first two items.

01/31/07 01:53:16 changed by Nikolaus Schlemm <nikl@nikl.net>

  • attachment include_reverse_relations.diff added.

just a quick option rename. unfortunately, I don't much more time right as I'm in the middle of starting a new job - plus moving from one city to another...

01/31/07 02:01:45 changed by Nikolaus Schlemm <nikl@nikl.net>

  • attachment include_reverse_relations.2.diff added.

argh! forgot django/core/serializers/python.py in the last diff...


Add/Change #2843 ([patch] serializer can't resolve a one-to-many relation from the "foreign" side)




Change Properties
Action