On Tue, May 23, 2017 at 3:44 PM, <jjanderson52...@gmail.com> wrote:

> I have a model class, 'A_base', and a 2nd model class, 'A_derived'. When I
> save an object of A_derived, I want the data from both my base and derived
> class to be saved.
>
> Example code looks like this:
>
>     class A_base(models.model):
>         name = models.CharField(max_length=50)
>
>     class A_derived(A_base):
>         role = models.CharField(max_length=50)
>
>
> So when I call save(), I want to save an instance of the derived object,
> including the data in the base class into my sqlite3 database. I would like
> my code to look like this:
>
>     ...
>     derived_obj = A_derived.objects.get(name="john")
>     derived_obj.role = "parent"
>     derived_obj.save()
>     ...
>
> My question is whether the save method will save 'role' to the A_derived
> table and save 'name' to the A_base table? Or do I have to override the
> save method so that it saves 'role' and then calls the base class save
> method to save 'name'?
>
>
No, you have created two separate (but related) models. A_base has a model
field of 'name', and A_derived has model fields 'name' and 'role'. Since
you are inheriting from a non-abstract parent model, there is an implied
OneToOne relationship between the two models, see the following:
https://docs.djangoproject.com/en/1.11/topics/db/models/#multi-table-inheritance

I'm not a big fan of this implied behavior, but it hasn't been problematic
for me because I make heavy use of abstract model classes.

If you do not want the implied relationship between the two models, create
an abstract base class matching A_base and have both of your models inherit
from it (A_base would be an empty model in this case, most likely).

>From a Django perspective, these two are completely independent
models/entities (aside from the OneToOne relationship). A_derivied will
have its own name and role. A_base also has it's own name, but will not be
able to contain a role.

To achieve the behavior you want, when A_derived is saved, you'll need to
either create a new A_base object and set the 'name' field to the same
value, or query for an existing A_base object and change/save the 'name'
field. Keep in mind that the two values are not connected to each other.
Changing the 'name' field on the same A_derived class later will NOT change
the 'name' field on an A_base object automatically, you would need to
provide such logic on one or both models, depending on the direction of the
dependency you wish to create.


> I tried doing this using the default save method and it created a stack
> trace, so I'm guessing that I am expecting too much from the default save
> method.
>
>
Post the entire stack trace and we may be able to better illuminate the
issue.

-James

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciXDpbGmmpqbcDC_iipvRVowJhuw3VcdeJsVLMAVJ5g-WQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to