Sunday, October 5, 2014

Bug in update_or_create?

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

--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0c38a484-bd0c-4769-91ee-50df5df47750%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment