0

I see baffling and what seems flatout bad behavior in serializing django objects. For example, I have models:

class MyTag(TagBase):
    user = models.ForeignKey(User)


class MyMpttTag(MPTTModel, MyTag):
    parent      = TreeForeignKey('self', null=True, blank=True, related_name='children')

    class MPTTMeta:
        parent_attr         = 'parent'

which means that MyMpptTag has fields of name, slug, user, parent. But when I do serializers.serialize('json', MyMpptTag.object.all()), I get: [{"fields": {"lft": 1, "level": 0, "tree_id": 29, "parent": null, "rght": 2}, "model": "index.mymptttag", "pk": 45}...]

Why would I lose name, slug, and user, and how do I get them back? Thank you

codyc4321
  • 9,014
  • 22
  • 92
  • 165
  • 1
    If `MyTag` is not an abstract class, serializing will result in at least 2 typw of objects (with same primary key). One of them will be from `MyMpttTag` model and one from `MyTag` model (there may be more if `TagBase` and it's parents are also non-abstract). – GwynBleidD Aug 16 '15 at 23:11
  • this makes more sense, so the normal behavior is good, but if you intended on using the class only as a subclass I should put abstract=True. will this cause any significant issues in migrating after I make is an abstract? – codyc4321 Aug 16 '15 at 23:13
  • TagBase is an abstract within django-taggit – codyc4321 Aug 16 '15 at 23:13
  • yes there was bad design when I did not declare abstract. I cannot accept your answer since it is in a comment – codyc4321 Aug 17 '15 at 20:38

1 Answers1

1

On that model design, you will have 2 tables in database:

  • yourapp_mytag that will have primary key column (normal auto-increment column), all columns inherited from TagBase (as long as TagBase is abstract) and column user - foreign key to model User
  • yourapp_mymptttag tht will have primary key column that is also foreign key to MyTag model and columns for mptt. There won't be any columns inherited from MyTag.

That means: there are no columns inherited from MyTag in model MyMpttTag, there are only references to actual columns in MyTag.

In serialization there will be 2 type of objects: MyMpttTag and MyTag.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77