Python django.db.models.SET_DEFAULT Examples

The following are 4 code examples for showing how to use django.db.models.SET_DEFAULT(). 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: django-htk   Author: hacktoolkit   File: 0003_auto__add_htkshorturl__add_htkshorturlaccess.py    License: MIT License 6 votes vote down vote up
def forwards(self, orm):
        # Adding model 'HTKShortUrl'
        db.create_table(u'htk_htkshorturl', (
            (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('url', self.gf('django.db.models.fields.CharField')(max_length=256)),
            ('creator', self.gf('django.db.models.fields.related.ForeignKey')(related_name='short_urls', on_delete=models.SET_DEFAULT, default=None, to=orm['auth.User'], blank=True, null=True)),
            ('created_on', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2015, 2, 21, 0, 0), auto_now_add=True, blank=True)),
        ))
        db.send_create_signal('htk', ['HTKShortUrl'])

        # Adding model 'HTKShortUrlAccess'
        db.create_table(u'htk_htkshorturlaccess', (
            (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
            ('url', self.gf('django.db.models.fields.related.ForeignKey')(related_name='accesses', to=orm['htk.HTKShortUrl'])),
            ('timestamp', self.gf('django.db.models.fields.DateTimeField')(auto_now=True, blank=True)),
            ('user', self.gf('django.db.models.fields.related.ForeignKey')(default=None, to=orm['auth.User'], null=True, on_delete=models.SET_DEFAULT, blank=True)),
            ('user_agent', self.gf('django.db.models.fields.CharField')(max_length=256, blank=True)),
            ('user_ip', self.gf('django.db.models.fields.CharField')(max_length=15, blank=True)),
            ('referrer', self.gf('django.db.models.fields.CharField')(max_length=256, blank=True)),
        ))
        db.send_create_signal('htk', ['HTKShortUrlAccess']) 
Example 3
Project: djongo   Author: nesdis   File: test_relative_fields.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_on_delete_set_default_without_default_value(self):
        class Person(models.Model):
            pass

        class Model(models.Model):
            foreign_key = models.ForeignKey('Person', models.SET_DEFAULT)

        field = Model._meta.get_field('foreign_key')
        self.assertEqual(field.check(), [
            Error(
                'Field specifies on_delete=SET_DEFAULT, but has no default value.',
                hint='Set a default value, or change the on_delete rule.',
                obj=field,
                id='fields.E321',
            ),
        ]) 
Example 4
Project: djongo   Author: nesdis   File: test_relative_fields.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_on_delete_set_default_without_default_value(self):
        class Person(models.Model):
            pass

        class Model(models.Model):
            foreign_key = models.ForeignKey('Person', models.SET_DEFAULT)

        field = Model._meta.get_field('foreign_key')
        self.assertEqual(field.check(), [
            Error(
                'Field specifies on_delete=SET_DEFAULT, but has no default value.',
                hint='Set a default value, or change the on_delete rule.',
                obj=field,
                id='fields.E321',
            ),
        ])