Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_or_create and automatic routing #76

Open
M1ha-Shvn opened this issue Jan 24, 2018 · 1 comment
Open

update_or_create and automatic routing #76

M1ha-Shvn opened this issue Jan 24, 2018 · 1 comment

Comments

@M1ha-Shvn
Copy link

Hi.
Model.objects.update_or_create(key_field=key_value, defaults={}) is not routed automatically, when value existed. The reason is in it's realization:

    def update_or_create(self, defaults=None, **kwargs):
        """
        Looks up an object with the given kwargs, updating one with defaults
        if it exists, otherwise creates a new one.
        Returns a tuple (object, created), where created is a boolean
        specifying whether an object was created.
        """
        defaults = defaults or {}
        lookup, params = self._extract_model_params(defaults, **kwargs)
        self._for_write = True
        try:
            obj = self.get(**lookup)
        except self.model.DoesNotExist:
            obj, created = self._create_object_from_params(lookup, params)
            if created:
                return obj, created
        for k, v in six.iteritems(defaults):
            setattr(obj, k, v)
        obj.save(using=self.db)
        return obj, False

obj has been got correctly, but self.db returns 'default'. As a result obj.save(using=self.db) fails.

@JBKahn
Copy link
Owner

JBKahn commented Jan 24, 2018

I can take a look. To be honest, I haven't done too much work on the routing myself but I know how it works. I know there are some flaws with Django around self.db state as it is a single field that can only track where something is rather than where it will be and will sometimes throw away the using arg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants