本文整理匯總了Python中django.db.models.SET_NULL屬性的典型用法代碼示例。如果您正苦於以下問題:Python models.SET_NULL屬性的具體用法?Python models.SET_NULL怎麽用?Python models.SET_NULL使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類django.db.models
的用法示例。
在下文中一共展示了models.SET_NULL屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ask_remove_enum_values
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
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
示例2: test_on_delete_set_null_on_non_nullable_field
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_on_delete_set_null_on_non_nullable_field(self):
class Person(models.Model):
pass
class Model(models.Model):
foreign_key = models.ForeignKey('Person', models.SET_NULL)
field = Model._meta.get_field('foreign_key')
self.assertEqual(field.check(), [
Error(
'Field specifies on_delete=SET_NULL, but cannot be null.',
hint='Set null=True argument on the field, or change the on_delete rule.',
obj=field,
id='fields.E320',
),
])
示例3: test_to_fields_not_checked_if_related_model_doesnt_exist
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_to_fields_not_checked_if_related_model_doesnt_exist(self):
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
parent = ForeignObject(
'invalid_models_tests.Parent',
on_delete=models.SET_NULL,
from_fields=('a', 'b'),
to_fields=('a', 'b'),
)
field = Child._meta.get_field('parent')
self.assertEqual(field.check(), [
Error(
"Field defines a relation with model 'invalid_models_tests.Parent', "
"which is either not installed, or is abstract.",
id='fields.E300',
obj=field,
),
])
示例4: test_check_composite_foreign_object
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_check_composite_foreign_object(self):
class Parent(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
class Meta:
unique_together = (('a', 'b'),)
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
value = models.CharField(max_length=255)
parent = ForeignObject(
Parent,
on_delete=models.SET_NULL,
from_fields=('a', 'b'),
to_fields=('a', 'b'),
related_name='children',
)
self.assertEqual(Child._meta.get_field('parent').check(from_model=Child), [])
示例5: test_check_subset_composite_foreign_object
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_check_subset_composite_foreign_object(self):
class Parent(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
c = models.PositiveIntegerField()
class Meta:
unique_together = (('a', 'b'),)
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
c = models.PositiveIntegerField()
d = models.CharField(max_length=255)
parent = ForeignObject(
Parent,
on_delete=models.SET_NULL,
from_fields=('a', 'b', 'c'),
to_fields=('a', 'b', 'c'),
related_name='children',
)
self.assertEqual(Child._meta.get_field('parent').check(from_model=Child), [])
示例6: forwards
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def forwards(self, orm):
# Changing field 'Location.photo'
db.alter_column(u'djangocms_gmaps_location', 'photo_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, on_delete=models.SET_NULL, to=orm['filer.Image']))
# Changing field 'Location.marker_icon'
db.alter_column(u'djangocms_gmaps_location', 'marker_icon_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, on_delete=models.SET_NULL, to=orm['filer.Image']))
開發者ID:mishbahr,項目名稱:djangocms-gmaps,代碼行數:9,代碼來源:0002_auto__chg_field_location_photo__chg_field_location_marker_icon.py
示例7: backwards
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def backwards(self, orm):
# Adding model 'MaterialShapeNicenessLabel'
db.create_table(u'shapes_materialshapenicenesslabel', (
('quality_method', self.gf('django.db.models.fields.CharField')(max_length=1, null=True, blank=True)),
('admin_score', self.gf('django.db.models.fields.IntegerField')(default=0)),
('time_active_ms', self.gf('django.db.models.fields.IntegerField')(blank=True, null=True, db_index=True)),
('canttell', self.gf('django.db.models.fields.NullBooleanField')(null=True, blank=True)),
('shape', self.gf('django.db.models.fields.related.ForeignKey')(related_name='nicenesses', to=orm['shapes.MaterialShape'])),
('added', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime.now)),
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['accounts.UserProfile'])),
(u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('mturk_assignment', self.gf('django.db.models.fields.related.ForeignKey')(related_name='+', null=True, to=orm['mturk.MtAssignment'], on_delete=models.SET_NULL, blank=True)),
('invalid', self.gf('django.db.models.fields.BooleanField')(default=False)),
('sandbox', self.gf('django.db.models.fields.BooleanField')(default=False)),
('time_ms', self.gf('django.db.models.fields.IntegerField')(blank=True, null=True, db_index=True)),
('reward', self.gf('django.db.models.fields.DecimalField')(null=True, max_digits=8, decimal_places=4, blank=True)),
('nice', self.gf('django.db.models.fields.BooleanField')(default=False)),
))
db.send_create_signal(u'shapes', ['MaterialShapeNicenessLabel'])
# Adding field 'MaterialShape.nice'
db.add_column(u'shapes_materialshape', 'nice',
self.gf('django.db.models.fields.NullBooleanField')(null=True, blank=True),
keep_default=False)
# Adding field 'MaterialShape.nice_score'
db.add_column(u'shapes_materialshape', 'nice_score',
self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
keep_default=False)
開發者ID:seanbell,項目名稱:opensurfaces,代碼行數:31,代碼來源:0002_auto__del_materialshapenicenesslabel__del_field_materialshape_nice__de.py
示例8: __init__
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def __init__(self, **kwargs):
kwargs["to"] = "files.File"
kwargs["on_delete"] = models.SET_NULL
kwargs["null"] = True
super().__init__(**kwargs)
示例9: test_post_redirect_page
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_post_redirect_page(self):
field = self.get_field(Form, "post_redirect_page")
self.assertModelPKField(field, Page, models.SET_NULL, True, True)
示例10: __init__
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def __init__(self, *args, **kwargs):
defaults = {
'verbose_name': _('Category'),
'to': Category,
'on_delete': models.SET_NULL,
'null': True,
'blank': True,
'related_name': '+',
}
defaults.update(kwargs)
super().__init__(**defaults)
示例11: test_serialize_functions
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_serialize_functions(self):
with self.assertRaisesMessage(ValueError, 'Cannot serialize function: lambda'):
self.assertSerializedEqual(lambda x: 42)
self.assertSerializedEqual(models.SET_NULL)
string, imports = MigrationWriter.serialize(models.SET(42))
self.assertEqual(string, 'models.SET(42)')
self.serialize_round_trip(models.SET(42))
示例12: test_to_fields_exist
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_to_fields_exist(self):
class Parent(models.Model):
pass
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
parent = ForeignObject(
Parent,
on_delete=models.SET_NULL,
from_fields=('a', 'b'),
to_fields=('a', 'b'),
)
field = Child._meta.get_field('parent')
self.assertEqual(field.check(), [
Error(
"The to_field 'a' doesn't exist on the related model 'invalid_models_tests.Parent'.",
obj=field,
id='fields.E312',
),
Error(
"The to_field 'b' doesn't exist on the related model 'invalid_models_tests.Parent'.",
obj=field,
id='fields.E312',
),
])
示例13: test_superset_foreign_object
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_superset_foreign_object(self):
class Parent(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
c = models.PositiveIntegerField()
class Meta:
unique_together = (('a', 'b', 'c'),)
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
value = models.CharField(max_length=255)
parent = ForeignObject(
Parent,
on_delete=models.SET_NULL,
from_fields=('a', 'b'),
to_fields=('a', 'b'),
related_name='children',
)
field = Child._meta.get_field('parent')
self.assertEqual(field.check(from_model=Child), [
Error(
"No subset of the fields 'a', 'b' on model 'Parent' is unique.",
hint=(
"Add unique=True on any of those fields or add at least "
"a subset of them to a unique_together constraint."
),
obj=field,
id='fields.E310',
),
])
示例14: test_intersection_foreign_object
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def test_intersection_foreign_object(self):
class Parent(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
c = models.PositiveIntegerField()
d = models.PositiveIntegerField()
class Meta:
unique_together = (('a', 'b', 'c'),)
class Child(models.Model):
a = models.PositiveIntegerField()
b = models.PositiveIntegerField()
d = models.PositiveIntegerField()
value = models.CharField(max_length=255)
parent = ForeignObject(
Parent,
on_delete=models.SET_NULL,
from_fields=('a', 'b', 'd'),
to_fields=('a', 'b', 'd'),
related_name='children',
)
field = Child._meta.get_field('parent')
self.assertEqual(field.check(from_model=Child), [
Error(
"No subset of the fields 'a', 'b', 'd' on model 'Parent' is unique.",
hint=(
"Add unique=True on any of those fields or add at least "
"a subset of them to a unique_together constraint."
),
obj=field,
id='fields.E310',
),
])
示例15: media_consumi
# 需要導入模塊: from django.db import models [as 別名]
# 或者: from django.db.models import SET_NULL [as 別名]
def media_consumi(self):
rifornimenti = Rifornimento.objects.filter(veicolo=self)
litri = 0
for rifornimento in rifornimenti:
litri += rifornimento.consumo_carburante
try:
ultimo_rifornimento = Rifornimento.objects.filter(veicolo=self).latest("data")
primo_rifornimento = Rifornimento.objects.filter(veicolo=self).earliest("data")
except Rifornimento.DoesNotExist:
return 0
km = ultimo_rifornimento.contachilometri - primo_rifornimento.contachilometri
litri -= ultimo_rifornimento.consumo_carburante
if litri != 0:
return round(km/litri, 2)
else:
return 0
# class Immatricolazione(ModelloSemplice, ConMarcaTemporale):
# """
# Rappresenta una pratica di immatricolazione di un Veicolo
#
# Una pratica viene istruita da un ufficio motorizzazione per conto di una unita' CRI richiedente.
# La stessa viene sottoposta a due stadi di approvazione, in seguito alla istruzione. Quando la
# pratica termina, il veicolo viene immatricolato ed entra in servizio.
# """
#
# class Meta:
# verbose_name = "Pratica di Immatricolazione"
# verbose_name_plural = "Pratiche di Immatricolazione"
#
# richiedente = models.ForeignKey(Sede, related_name='immatricolazioni_richieste', null=True, on_delete=models.SET_NULL)
# ufficio = models.ForeignKey(Sede, related_name='immatricolazioni_istruite', on_delete=models.PROTECT)
# veicolo = models.ForeignKey(Veicolo, related_name='richieste_immatricolazione', on_delete=models.CASCADE)