Fromisoformat: argument must be str Django

TypeError at /profile/updateAuction/updateData/1

fromisoformat: argument must be str

I have this problem when I want to update expiration date by the form, but it changes in admin panel. But I want to make changeable for user.

def updateData(request, id):
    title = request.POST['title']
    description = request.POST['description']
    expiration_date = (request.POST['expiration_date'], f'%m/%d/%Y')
    lot_cost = request.POST['lot_cost']
    image = request.POST['image']
    updatedUser = Auction.objects.get(id=id)
    updatedUser.description = description
    updatedUser.expiration_date = expiration_date
    updatedUser.lot_cost = lot_cost
    updatedUser.cover = image
    updatedUser.title = title
    return HttpResponseRedirect(reverse('index'))

class Auction(models.Model):
    title = models.CharField(max_length=255, null=False, blank=False)
    image = models.ImageField(upload_to='images', default='images/no-image.svg')
    description = models.TextField(null=False, blank=False)
    lot_cost = models.PositiveIntegerField(null=False, blank=False, default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    expiration_date = models.DateTimeField()
    user = models.ForeignKey(User, verbose_name='Пользователь', on_delete=models.CASCADE)


<form action="./updateData/{{ }}" method="post" class="row w-25">
     {% csrf_token %}
    Title: <input name="title" value="{{ Auction.title }}">
    Description: <input name="description" value="{{ Auction.description }}">
    Image: <input type="file" name="image" value="{{ Auction.cover }}">
    Cost: <input type="number" pattern="[0-9]" min="0" name="lot_cost" onkeypress="return event.charCode >= 48 && event.charCode <= 57" value="{{ Auction.lot_cost }}">
    Expiration date: <input type="date" name="expiration_date" value="{{ Auction.expiration_date }}">
    <input type="submit" value="update" name="submit">

The problem is that you're trying to set updatedUser.expiration_date to a tuple (request.POST['expiration_date'], f'%m/%d/%Y') instead of a datetime.datetime instance.

To accomplish this you should use datetime.datetime.strptime() and the format should be "%Y-%m-%d", like so:

import datetime

expiration_date = datetime.datetime.strptime(request.POST['expiration_date'], '%Y-%m-%d %H:%M:%S')

Also, it is better to use url tag instead of manually giving it in action attribute.


expiration_date is a DateTimeField and you are only saving date in it, you should use input of type="datetime-local in Html so that both date and time could be saved.

Back to Top