The child objects are created separately beforehand with NULL parents (they 
don't have to have parents, they can be orphans). One other thing I noticed 
is that looking at the SQL statements executed by my app I can't even see 
the UPDATE on the foreign keys of the children (even though I KNOW it's 
happening because I can go look at those children and see that they do get 
updated). It's as if Django doesn't even know about this at all.

On Sunday, February 10, 2019 at 10:54:10 AM UTC-5, Shashank Singh wrote:
>
> And how?
>
> On Sun, 10 Feb, 2019, 9:23 PM Shashank Singh <shashank....@gmail.com 
> <javascript:> wrote:
>
>> When do you create child objects??
>>
>> On Sun, 10 Feb, 2019, 9:17 PM Nick Emery <nicke...@gmail.com 
>> <javascript:> wrote:
>>
>>> Tried this too but save() never actually gets called on the child (it 
>>> seems that the foreign key field of the child is updated at the database 
>>> level and never goes through the Django orm Child).
>>>
>>> On Sunday, February 10, 2019 at 8:37:54 AM UTC-5, Shashank Singh wrote:
>>>>
>>>> Override the save() of the child?
>>>>
>>>> On Sun, 10 Feb, 2019, 7:04 PM Nick Emery <nicke...@gmail.com wrote:
>>>>
>>>>> I am trying to wrap up my first app using Django (specifically Django 
>>>>> Rest Framework which may change the save behavior), but have run into 
>>>>> an issue that I haven't been able to solve for about 10 hours now.
>>>>>
>>>>> I am trying to override the save() method of a model to modify a field 
>>>>> on a bunch of child objects when it is created; however, I can't figure 
>>>>> out 
>>>>> *any* way get the objects or ids for the children.
>>>>>
>>>>> My code looks something like:
>>>>> class Parent(models.Model):
>>>>>    """When I create this, I want the children to be modified."""
>>>>>    def save(self, *args, **kwargs):
>>>>>        if not self.pk:
>>>>>            self._begin(*args, **kwargs)
>>>>>        else:
>>>>>            super(Parent, self).save(*args, **kwargs)
>>>>>
>>>>>     def _begin(self, *args, **kwargs):
>>>>>        super(Parent, self).save(*args, **kwargs)
>>>>>        print(self.child_set.all())  # --> This gives: "<QuerySet []>"
>>>>>        for i in self.child_set.all():
>>>>>            i.last_name = self.last_name
>>>>>            i.save()
>>>>>
>>>>>     last_name = models.CharField(max_length=100)
>>>>>
>>>>>
>>>>> class Child:
>>>>>    parent = models.ForeignKey(
>>>>>        'Parent',
>>>>>        on_delete=models.SET_NULL,
>>>>>        null=True,
>>>>>     )
>>>>>    last_name = models.CharField(max_length=100)
>>>>>
>>>>> ANY way to modify the children when the parent is saved would be a 
>>>>> life saver (even if hacky or sub-optimal).
>>>>>
>>>>> -- 
>>>>> 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...@googlegroups.com.
>>>>> To post to this group, send email to django...@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/24c7156b-6873-4cd3-952e-dc819fa72be0%40googlegroups.com
>>>>>  
>>>>> <https://groups.google.com/d/msgid/django-users/24c7156b-6873-4cd3-952e-dc819fa72be0%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> -- 
>>> 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...@googlegroups.com <javascript:>.
>>> To post to this group, send email to django...@googlegroups.com 
>>> <javascript:>.
>>> 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/8dca6561-aea1-492d-b209-a9767ce30bd0%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/django-users/8dca6561-aea1-492d-b209-a9767ce30bd0%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>

-- 
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/32b133b9-9649-4490-afb9-87beb07af1e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to