當前位置: 首頁>>代碼示例>>Python>>正文


Python utils.DataError方法代碼示例

本文整理匯總了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 
開發者ID:openfun,項目名稱:richie,代碼行數:18,代碼來源:effort.py

示例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 
開發者ID:openfun,項目名稱:richie,代碼行數:21,代碼來源:effort.py

示例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 
開發者ID:openfun,項目名稱:richie,代碼行數:18,代碼來源:duration.py

示例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 
開發者ID:openfun,項目名稱:richie,代碼行數:21,代碼來源:duration.py

示例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) 
開發者ID:projectcaluma,項目名稱:caluma,代碼行數:26,代碼來源:test_migration.py

示例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") 
開發者ID:genialis,項目名稱:resolwe,代碼行數:14,代碼來源:test_fields.py

示例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.") 
開發者ID:codeforboston,項目名稱:cornerwise,代碼行數:32,代碼來源:tasks.py

示例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 
開發者ID:mmohrhard,項目名稱:crash,代碼行數:35,代碼來源:models.py

示例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") 
開發者ID:adamchainz,項目名稱:django-mysql,代碼行數:5,代碼來源:test_enumfield.py

示例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() 
開發者ID:adamchainz,項目名稱:django-mysql,代碼行數:5,代碼來源:test_enumfield.py

示例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 
開發者ID:adamchainz,項目名稱:django-mysql,代碼行數:12,代碼來源:test_size_fields.py

示例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 
開發者ID:adamchainz,項目名稱:django-mysql,代碼行數:12,代碼來源:test_size_fields.py

示例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",
        },
    ] 
開發者ID:getsentry,項目名稱:sentry-python,代碼行數:56,代碼來源:test_basic.py

示例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 
開發者ID:mangaki,項目名稱:mangaki,代碼行數:55,代碼來源:add_manga.py


注:本文中的django.db.utils.DataError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。