Friday, May 26, 2017

Re: saving data from both a model and a derived model


James,

I'm thinking about my last question more. If each object of the given type must be unique, there must be a manager in Django which keeps track of uniqueness. I will start searching for how I check for unique objects, but if you have a quick response, that will help.

Jim

On Tuesday, May 23, 2017 at 6:44:01 PM UTC-4, jjander...@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'?

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.

Jim A.

--
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+unsubscribe@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/9776663f-45a4-485d-9a2c-4e50c3e6e5f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment