本文整理匯總了Python中django.db.utils.DataError方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.DataError方法的具體用法?Python utils.DataError怎麽用?Python utils.DataError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.utils
的用法示例。
在下文中一共展示了utils.DataError方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: from_db_value
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def from_db_value(value, *_args):
"""Convert a database value to a list value."""
if not value:
return None
try:
duration, unit, reference = value.split("|")
except ValueError:
raise DataError(
"Value in database should be of the form '{duration:d}|{unit:s}|{reference:s}'"
)
if not duration:
return None
return int(duration), unit, reference
示例2: to_python
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def to_python(self, value):
"""Convert a string value to a list value. Used for deserialization and in clean forms."""
if isinstance(value, (list, tuple)):
return value
if not value:
return None
if isinstance(value, str):
try:
duration, unit, reference = value.split("|")
except ValueError:
raise DataError(
"Value in database should be of the form '{duration:d}|{unit:s}|{reference:s}'"
)
return int(duration.strip()), unit.strip(), reference.strip()
return value
示例3: from_db_value
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def from_db_value(value, *_args):
"""Convert a database value to a list value."""
if not value:
return None
try:
duration, unit = value.split("|")
except ValueError:
raise DataError(
"Value in database should be of the form '{duration:d}|{unit:s}'"
)
if not duration:
return None
return int(duration), unit
示例4: to_python
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def to_python(self, value):
"""Convert a string value to a list value. Used for deserialization and in clean forms."""
if isinstance(value, (list, tuple)):
return value
if not value:
return None
if isinstance(value, str):
try:
duration, unit = value.split("|")
except ValueError:
raise DataError(
"Value in database should be of the form '{duration:d}|{unit:s}'"
)
return int(duration.strip()), unit.strip()
return value
示例5: test_migrate_to_form_question_natural_key_reverse
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_migrate_to_form_question_natural_key_reverse(transactional_db):
executor = MigrationExecutor(connection)
app = "caluma_form"
migrate_from = [(app, "0024_auto_20190919_1244")]
migrate_to = [(app, "0023_auto_20190729_1448")]
executor.migrate(migrate_from)
old_apps = executor.loader.project_state(migrate_from).apps
# Create some old data. Can't use factories here
Form = old_apps.get_model(app, "Form")
Question = old_apps.get_model(app, "Question")
FormQuestion = old_apps.get_model(app, "FormQuestion")
form_1 = Form.objects.create(slug="form-1")
question_1 = Question.objects.create(type="text", slug="question-1")
FormQuestion.objects.create(form=form_1, question=question_1)
# Migrate backwards.
executor.loader.build_graph() # reload.
with pytest.raises(DataError):
executor.migrate(migrate_to)
示例6: test_predefined_long_slugs
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_predefined_long_slugs(self):
from .fields_test_app.models import TestModel
slug_max_length = TestModel._meta.get_field("slug").max_length
max_slug = "x" * slug_max_length
# Predefined slug shouldn't be trimmed
obj = TestModel.objects.create(name="Test object", slug=max_slug)
self.assertEqual(obj.slug, max_slug)
with self.assertRaisesRegex(DataError, "value too long"):
TestModel.objects.create(name="Test object", slug=max_slug + "y")
示例7: add_street_view
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def add_street_view(proposal: Proposal, logger=task_logger):
api_key = getattr(settings, "GOOGLE_API_KEY")
secret = getattr(settings, "GOOGLE_STREET_VIEW_SECRET")
if api_key:
location = "{0.address}, {0.region_name}".format(proposal)
url = street_view.street_view_url(location, api_key, secret=secret)
try:
# Check that there is not already a Street View image associated
# with this proposal:
if not proposal.images\
.filter(source="google_street_view")\
.exists():
image = proposal.images.create(
url=url,
skip_cache=True,
source="google_street_view",
height=640,
width=640,
priority=1)
return image
except IntegrityError:
logger.warning("Image with that URL already exists: %s", url)
except DataError:
logger.error(
"Image could not be saved. Was the URL too long? %s", url)
else:
logger.warn("Add a local_settings file with your Google API key "
"to add Street View images.")
示例8: _set_signature
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def _set_signature(self, frame_list):
text = ""
json_frame_list = json.loads(frame_list)
if len(json_frame_list) > 0:
frame = self._find_frame(json_frame_list)
function = frame['function']
if len(function) > 0:
text = function
else:
text = frame['lib_name']
if len(text) is 0:
text = "Invalid signature"
logger.warn("could not create a valid signature for %s" % self.crash_id)
text = text[:255] if len(text) > 255 else text
signatures = Signature.objects.filter(signature=text)
if len(signatures) < 1:
signature = Signature()
signature.signature = text
signature.first_observed = self.upload_time
signature.last_observed = self.upload_time
else:
signature = signatures[0]
if signature.last_observed < self.upload_time:
signature.last_observed = self.upload_time
try:
signature.save()
except DataError as e:
logger.error("error trying to save signature %s" % text)
logger.error(str(e))
self.signature = signature
示例9: test_invalid_value
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_invalid_value(self):
with pytest.raises(DataError):
EnumModel.objects.create(field="elephant")
示例10: test_empty
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_empty(self):
with pytest.raises(DataError):
EnumModel.objects.create()
示例11: test_binary_1_max_length
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_binary_1_max_length(self):
# Okay
m = SizeFieldModel(binary1=bytes(1) * (2 ** 8 - 1))
m.save()
# Bad - Data too long
m = SizeFieldModel(binary1=bytes(1) * (2 ** 8))
with pytest.raises(DataError) as excinfo:
m.save()
assert excinfo.value.args[0] == 1406
示例12: test_tinytext_max_length
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_tinytext_max_length(self):
# Okay
m = SizeFieldModel(text1="a" * (2 ** 8 - 1))
m.save()
# Bad - Data too long
m = SizeFieldModel(text1="a" * (2 ** 8))
with atomic(), pytest.raises(DataError) as excinfo:
m.save()
assert excinfo.value.args[0] == 1406
示例13: test_sql_psycopg2_placeholders
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def test_sql_psycopg2_placeholders(sentry_init, capture_events):
sentry_init(
integrations=[DjangoIntegration()],
send_default_pii=True,
_experiments={"record_sql_params": True},
)
from django.db import connections
if "postgres" not in connections:
pytest.skip("postgres tests disabled")
import psycopg2.sql
sql = connections["postgres"].cursor()
events = capture_events()
with pytest.raises(DataError):
names = ["foo", "bar"]
identifiers = [psycopg2.sql.Identifier(name) for name in names]
placeholders = [
psycopg2.sql.Placeholder(var) for var in ["first_var", "second_var"]
]
sql.execute("create table my_test_table (foo text, bar date)")
query = psycopg2.sql.SQL("insert into my_test_table ({}) values ({})").format(
psycopg2.sql.SQL(", ").join(identifiers),
psycopg2.sql.SQL(", ").join(placeholders),
)
sql.execute(query, {"first_var": "fizz", "second_var": "not a date"})
capture_message("HI")
(event,) = events
for crumb in event["breadcrumbs"]:
del crumb["timestamp"]
assert event["breadcrumbs"][-2:] == [
{
"category": "query",
"data": {"db.paramstyle": "format"},
"message": "create table my_test_table (foo text, bar date)",
"type": "default",
},
{
"category": "query",
"data": {
"db.params": {"first_var": "fizz", "second_var": "not a date"},
"db.paramstyle": "format",
},
"message": 'insert into my_test_table ("foo", "bar") values (%(first_var)s, '
"%(second_var)s)",
"type": "default",
},
]
示例14: run
# 需要導入模塊: from django.db import utils [as 別名]
# 或者: from django.db.utils import DataError [as 別名]
def run():
with open('../data/manga-news/manga.csv') as f:
next(f)
artists = {}
hipsters = Counter()
for i, line in enumerate(f):
# print(len(line.split(';;')))
title, vo_title, writer, mangaka, editor, origin, genre1, genre2, manga_type, synopsis, poster = line.split(';;')
for artist in [writer, mangaka]:
if artist in artists:
continue
m = re.match('^([A-ZÔÛÏ\'-]+) (.*)$', writer)
if m:
last_name, first_name = m.groups()
last_name = last_name.lower().capitalize()
if not m:
first_name = ''
last_name = artist
if Artist.objects.filter(first_name=first_name, last_name=last_name).count() == 0:
a = Artist(first_name=first_name, last_name=last_name)
a.save()
else:
a = Artist.objects.get(first_name=first_name, last_name=last_name)
artists[artist] = a
with open('../data/manga-news/manga.csv') as f:
next(f)
for i, line in enumerate(f):
title, vo_title, writer, mangaka, editor, origin, genre1, genre2, manga_type, synopsis, poster = line.split(';;')
try:
if Manga.objects.filter(title=title, vo_title=vo_title).count() == 0:
manga = Manga(title=title, vo_title=vo_title, mangaka=artists[mangaka], writer=artists[writer], editor=editor, origin=origin.lower().replace('hong kong', 'hong-kong').replace('international', 'intl'), manga_type=manga_type.lower(), source='', poster=poster, synopsis=synopsis)
manga.save()
else:
manga = Manga.objects.get(title=title, vo_title=vo_title)
if genre1:
manga.genre.add(Genre.objects.get(title=genre1))
if genre2:
manga.genre.add(Genre.objects.get(title=genre2))
except IntegrityError as err:
print(line)
print(writer)
print(err)
break
except DataError as err:
print(line)
print(origin)
print(err)
break
except Genre.DoesNotExist as err:
print(line)
print('Genres: [%s] [%s]' % (genre1, genre2))
print(err)
break