Bug in update_or_create?

47 views
Skip to first unread message

Ryan Hiebert

unread,
Oct 5, 2014, 11:43:22 PM10/5/14
to django...@googlegroups.com
On related managers, in particular for my case reverse foreign keys, update_or_create doesn't appear to retain the knowledge of the model from whence it came.

Is this an expected behavior, or is it a bug?

models.py:

from django.db import models

class Spam(models.Model):
    pass

class Egg(models.Model):
    spam = models.ForeignKey(Spam, related_name='eggs')

tests.py:

from django.test import TestCase

from .models import Spam, Egg

class TestUpdateOrCreate(TestCase):
    def test_update_or_create_on_model_manager(self):
        spam = Spam.objects.create()
        Egg.objects.update_or_create(id=7, defaults={'spam': spam})

    def test_update_or_create_on_related_manager(self):
        spam = Spam.objects.create()
        spam.eggs.update_or_create(id=8)

The first works, as it manually specifies the required 'spam' property on the Egg. The second fails. I would have expected it to know that it's coming from the spam instance, and thus have the foreign key set appropriately. Am I misunderstanding?

Ryan
Reply all
Reply to author
Forward
0 new messages