2

I'm trying to update two datetimefields for the Skill entity in my db. I'm sure I'm passing an isoformat datetime string. When creating instead of updating everything works fine but on the update I get:

Exception Type:     TypeError
Exception Value:    fromisoformat: argument must be str

Here is the code raising the TypeError:

# create or update skill
    created = ibm_date_to_iso(clean_data["skill"]["created"])
    updated = ibm_date_to_iso(clean_data["skill"]["updated"])
    try:
        skill = Skill.objects.get(
            skill_id=clean_data["skill"]["skill_id"],
            snapshot=clean_data["skill"]["snapshot"]
        )

        # update fields if skills exists already
        skill.name = clean_data["skill"]["name"],
        skill.type = clean_data["skill"]["type"],
        skill.status = clean_data["skill"]["status"],
        skill.created = created,
        skill.updated = updated,
        skill.language = clean_data["skill"]["language"],
        skill.description = clean_data["skill"]["description"],
        skill.dialog_settings = clean_data["skill"]["dialog_settings"]
        skill.uploads.add(u)
    except Skill.DoesNotExist:
        skill = Skill(
            skill_id=clean_data["skill"]["skill_id"],
            name=clean_data["skill"]["name"],
            type=clean_data["skill"]["type"],
            status=clean_data["skill"]["status"],
            created=created,
            updated=updated,
            language=clean_data["skill"]["language"],
            snapshot=clean_data["skill"]["snapshot"],
            description=clean_data["skill"]["description"],
            dialog_settings=clean_data["skill"]["dialog_settings"]
        )
        skill.save()
        skill.uploads.add(u)
        # new page for the skill entity
        page = Page(
            title="notes for Skill {} snap {}".format(
                skill.name,
                skill.snapshot),
            content="This page is empty..."
        )
        page.save()
    skill.save()

The exception is raised on the last line (skill.save()) only when the DoesNotExist exception is not raised.

Here is the Skill model:

class Skill(models.Model):
    skill_id = models.CharField(max_length=36)
    name = models.TextField()
    type = models.TextField()
    status = models.TextField()
    created = models.DateTimeField()
    updated = models.DateTimeField()
    language = models.TextField()
    snapshot = models.IntegerField()
    description = models.TextField()
    dialog_settings = models.TextField()
    page = models.ForeignKey(
        "Page",
        on_delete=models.SET_NULL,
        default=None,
        null=True,
        blank=True)
    uploads = models.ManyToManyField(
        "upload",
        blank=True,
        default=None)

    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=["skill_id", "snapshot"],
                name="unique_skill_snapshot")
        ]

When inspecting the debugging data the ultimate line that raises the TypeError is in the Django codebase:

assistant_doc/python_env/lib/python3.8/site-packages/django/utils/dateparse.py, line 116, in parse_datetime

            return datetime.datetime.fromisoformat(value)

     …

Local vars
Variable    Value
value   ('2022-01-21T10:07:32.045000',)

As you can see the 'value' parameter has been changed to a tuple containing the isoformat string. Is this a bug in Django?

1 Answers1

3

I had the same problem. It turned out that I had a trailing comma at the end of the assignment statement.

In your code the error is here:

skill.name = clean_data["skill"]["name"], # <-- comma here!

Python creates a tuple when a comma is at the end of a variable.

Devesh
  • 603
  • 2
  • 11
Zakoulov
  • 131
  • 4