Python django.db.models.DO_NOTHING Examples

The following are 14 code examples for showing how to use django.db.models.DO_NOTHING(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.db.models , or try the search function .

Example 1
Project: django-more   Author: ashleywaite   File: patches.py    License: BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def ask_remove_enum_values(self, db_type, values):
        """ How to treat records with deleted enum values. """
        # Ordered ensures
        choices = [
            (models.CASCADE, "Cascade - Delete records with removed values"),
            (models.PROTECT, "Protect - Block migrations if records contain removed values"),
            (models.SET_NULL, "Set NULL - Set value to NULL"),
            (models.SET_DEFAULT, "Set default - Set value to field default"),
            (models.SET, "Set value - Provide a one off default now"),
            (models.DO_NOTHING, "Do nothing - Consistency must be handled elsewhere"),
            (None, "Leave it to field definitions")]
        choice, _ = choices[self._choice_input(
            "Enum {db_type} has had {values} removed, "
            "existing records may need to be updated. "
            "Override update behaviour or do nothing and follow field behaviour.".format(
                db_type=db_type,
                values=values),
            [q for (k, q) in choices]) - 1]
        if choice == models.SET:
            return models.SET(self._ask_default())
        return choice 
Example 2
Project: jeeves   Author: jeanqasaur   File: JeevesModel.py    License: MIT License 6 votes vote down vote up
def __init__(self, to, *args, **kwargs):
        self.to = to
        if (isinstance(to,basestring)):
            super(JeevesForeignKey, self).__init__(
                    to, kwargs.pop("on_delete",models.DO_NOTHING), kwargs.pop("from_fields",[]), kwargs.pop("to_fields",[]), *args, **kwargs)
        else:
            self.join_field = to._meta.pk
            for field in to._meta.fields:
                if field.name == 'jeeves_id':
                    self.join_field = field
                    break
                else:
                    # support non-Jeeves tables
                    self.join_field = to._meta.pk
                    #raise Exception("Need jeeves_id field")

            super(JeevesForeignKey, self).__init__(
                    to, models.DO_NOTHING, [self], [self.join_field], *args, **kwargs)
        self.db_constraint = False 
Example 3
Project: GTDWeb   Author: lanbing510   File: fields.py    License: GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, field, to, related_name=None, limit_choices_to=None, related_query_name=None):
        super(GenericRel, self).__init__(field=field, to=to, related_name=related_query_name or '+',
                                         limit_choices_to=limit_choices_to, on_delete=DO_NOTHING,
                                         related_query_name=related_query_name) 
Example 4
Project: dingtalk-django-example   Author: 007gzs   File: model.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, to, on_delete=models.DO_NOTHING, related_name=None, related_query_name=None,
                 limit_choices_to=None, parent_link=False, to_field=None, db_constraint=False,  **kwargs):
        super(ForeignKey, self).__init__(to, on_delete, related_name, related_query_name, limit_choices_to,
                                         parent_link, to_field, db_constraint, **kwargs) 
Example 5
Project: dingtalk-django-example   Author: 007gzs   File: model.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, to, on_delete=models.DO_NOTHING, to_field=None, db_constraint=False, **kwargs):
        super(OneToOneField, self).__init__(to, on_delete, to_field, db_constraint=db_constraint, **kwargs) 
Example 6
Project: pasportaservo   Author: tejoesperanto   File: fields.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, verbose_name=None, choices=None, to_field=None, **kwargs):
        self.suggestions_source_field = to_field
        self.suggestions = choices
        kwargs.update(
            db_constraint=False, null=True, on_delete=models.DO_NOTHING,
            to=self.suggestions, to_field=to_field, verbose_name=verbose_name)
        super().__init__(**kwargs) 
Example 7
Project: meeting   Author: 007gzs   File: utils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, to, on_delete=models.DO_NOTHING, related_name=None, related_query_name=None,
                 limit_choices_to=None, parent_link=False, to_field=None, db_constraint=False,  **kwargs):
        super(ForeignKey, self).__init__(to, on_delete, related_name, related_query_name, limit_choices_to,
                                         parent_link, to_field, db_constraint, **kwargs) 
Example 8
Project: meeting   Author: 007gzs   File: utils.py    License: GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, to, on_delete=models.DO_NOTHING, to_field=None, db_constraint=False, **kwargs):
        super(OneToOneField, self).__init__(to, on_delete, to_field, db_constraint=db_constraint, **kwargs) 
Example 9
Project: openhgsenti   Author: drexly   File: fields.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, field, to, related_name=None, related_query_name=None, limit_choices_to=None):
        super(GenericRel, self).__init__(
            field, to,
            related_name=related_query_name or '+',
            related_query_name=related_query_name,
            limit_choices_to=limit_choices_to,
            on_delete=DO_NOTHING,
        ) 
Example 10
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_do_nothing(self):
        # Testing DO_NOTHING is a bit harder: It would raise IntegrityError for a normal model,
        # so we connect to pre_delete and set the fk to a known value.
        replacement_r = R.objects.create()

        def check_do_nothing(sender, **kwargs):
            obj = kwargs['instance']
            obj.donothing_set.update(donothing=replacement_r)
        models.signals.pre_delete.connect(check_do_nothing)
        a = create_a('do_nothing')
        a.donothing.delete()
        a = A.objects.get(pk=a.pk)
        self.assertEqual(replacement_r, a.donothing)
        models.signals.pre_delete.disconnect(check_do_nothing) 
Example 11
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_do_nothing_qscount(self):
        """
        A models.DO_NOTHING relation doesn't trigger a query.
        """
        b = Base.objects.create()
        with self.assertNumQueries(1):
            # RelToBase should not be queried.
            b.delete()
        self.assertEqual(Base.objects.count(), 0) 
Example 12
Project: djongo   Author: nesdis   File: empty_join.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        kwargs['on_delete'] = models.DO_NOTHING
        super().__init__(*args, **kwargs) 
Example 13
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_do_nothing(self):
        # Testing DO_NOTHING is a bit harder: It would raise IntegrityError for a normal model,
        # so we connect to pre_delete and set the fk to a known value.
        replacement_r = R.objects.create()

        def check_do_nothing(sender, **kwargs):
            obj = kwargs['instance']
            obj.donothing_set.update(donothing=replacement_r)
        models.signals.pre_delete.connect(check_do_nothing)
        a = create_a('do_nothing')
        a.donothing.delete()
        a = A.objects.get(pk=a.pk)
        self.assertEqual(replacement_r, a.donothing)
        models.signals.pre_delete.disconnect(check_do_nothing) 
Example 14
Project: djongo   Author: nesdis   File: tests.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_do_nothing_qscount(self):
        """
        A models.DO_NOTHING relation doesn't trigger a query.
        """
        b = Base.objects.create()
        with self.assertNumQueries(1):
            # RelToBase should not be queried.
            b.delete()
        self.assertEqual(Base.objects.count(), 0)