Opened 6 years ago

Closed 6 years ago

#29650 closed Bug (needsinfo)

Model.refresh_from_db() no longer refreshes related objects

Reported by: Thomas Berdy Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords: refresh_from_db
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

After updating to Django 2.1 from Django 2.0.7, I noticed one of my tests failed. After further investigation, I figured out the problem was coming from refresh_from_db method. Here is my test:

    s = MyCustomSerializer()
    order = s.create(copy.deepcopy(order_data))

    # Check shipping_address exists
    assert order.shipping_address

    order_data["shipping_address"]["city"] = "Sevran"
    s.update(order, copy.deepcopy(order_data))
    order.refresh_from_db()

    # Check order's shipping_address has been updated
    assert order.shipping_address.city == "Sevran"

Here you can find the definition (light version) of the Order model:

class Order(TimeStampedModel):
    shipping_address = models.ForeignKey(
        "order.ShippingAddress", null=True, blank=True,
        verbose_name=_("Shipping Address"), on_delete=models.SET_NULL
    )

Before 2.1, refresh_from_db refresh the whole object and the objects linked by foreign key.
Now it seems that only the first-level object is refreshed.
Is it normal ? Maybe you can mention it in the release notes.

Change History (2)

comment:1 by Tim Graham, 6 years ago

Can you simplify your steps to reproduce with some minimal models and without MyCustomSerializer and the deepcopy stuff? It's difficult to tell exactly what's happening in your code. Ideally, you could also bisect to find the commit where the behavior changed. 136bf5c2142ae3261669d02e2dd050c3141f7f2f perhaps.

comment:2 by Tim Graham, 6 years ago

Resolution: needsinfo
Status: newclosed
Summary: Changes in refresh_from_db methodModel.refresh_from_db() no longer refreshes related objects
Type: UncategorizedBug

For existing refresh_from_db() tests, look at tests/basic/tests.py. I can't reproduce a problem based on the details you provided.

Note: See TracTickets for help on using tickets.
Back to Top