| | 53 | |
|---|
| | 54 | # |
|---|
| | 55 | # Regression test for #6886 (the related-object cache) |
|---|
| | 56 | # |
|---|
| | 57 | |
|---|
| | 58 | # Look up the objects again so that we get "fresh" objects |
|---|
| | 59 | >>> p = Place.objects.get(name="Demon Dogs") |
|---|
| | 60 | >>> r = p.restaurant |
|---|
| | 61 | |
|---|
| | 62 | # Accessing the related object again returns the exactly same object |
|---|
| | 63 | >>> p.restaurant is r |
|---|
| | 64 | True |
|---|
| | 65 | |
|---|
| | 66 | # But if we kill the cache, we get a new object |
|---|
| | 67 | >>> del p._restaurant_cache |
|---|
| | 68 | >>> p.restaurant is r |
|---|
| | 69 | False |
|---|
| | 70 | |
|---|
| | 71 | # Reassigning the Restaurant object results in an immediate cache update |
|---|
| | 72 | # We can't use a new Restaurant because that'll violate one-to-one, but |
|---|
| | 73 | # with a new *instance* the is test below will fail if #6886 regresses. |
|---|
| | 74 | >>> r2 = Restaurant.objects.get(pk=r.pk) |
|---|
| | 75 | >>> p.restaurant = r2 |
|---|
| | 76 | >>> p.restaurant is r2 |
|---|
| | 77 | True |
|---|
| | 78 | |
|---|
| | 79 | # Assigning None fails: Place.restaurant is null=False |
|---|
| | 80 | >>> p.restaurant = None |
|---|
| | 81 | Traceback (most recent call last): |
|---|
| | 82 | ... |
|---|
| | 83 | ValueError: Cannot assign None: "Place.restaurant" does not allow null values. |
|---|
| | 84 | |
|---|
| | 85 | # You also can't assign an object of the wrong type here |
|---|
| | 86 | >>> p.restaurant = p |
|---|
| | 87 | Traceback (most recent call last): |
|---|
| | 88 | ... |
|---|
| | 89 | ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance. |
|---|
| | 90 | |
|---|