0

I'm trying to create a new record in my Django model but I just can't. Here is the Sell class model:

class Sell(models.Model):
    item = models.OneToOneField(Buy, related_name='sell', on_delete=models.CASCADE)
    date = models.DateField(default=timezone.now)
    discount = models.DecimalField(max_digits=6, decimal_places=2)
    total_paid = models.DecimalField(max_digits=6, decimal_places=2)
    buyer = models.ForeignKey(User, related_name='sell', on_delete=models.CASCADE)

Here is the code in my view.py:

    Sell.objects.create(item=item,
                        date=timezone.now,
                        discount=Decimal(0.00),
                        total_paid=cart.get_total_price(),
                        buyer=request.user, )

If I print all of this variables, the result is:

    print(item)
    266
    print(type(item))
    <class 'dma.models.Buy'>
    
    print(timezone.now)
    <function datetime.date at 0x03D3F460>
    print(type(timezone.now))
    <class 'function'>
    
    print(type(Decimal(0.00)))
    <class 'decimal.Decimal'>
    
    print(cart.get_total_price())
    68.00
    print(type(cart.get_total_price()))
    <class 'decimal.Decimal'>
    
    print(request.user)
    Felipe
    print(type(request.user))
    <class 'django.utils.functional.SimpleLazyObject'>

Error:

Exception Type: TypeError Exception Value:
expected string or bytes-like object

enter image description here

4
  • 1
    The code in your image is not the same as in your question. Commented Jul 30, 2020 at 10:58
  • @AKX It is the same he just reordered it , Like the create function he put it before the print statements on SO but in his code it came last . Commented Jul 30, 2020 at 11:00
  • 1
    @RaoufM No, it's not. The traceback shows date=datetime.date, the pasted code doesn't. Commented Jul 30, 2020 at 11:33
  • Guys, thank you! The code is now working. I should've made some mistaken and the initial code: Sell.objects.create(item=item, date=timezone.now(), discount=Decimal(0.00), total_paid=cart.get_total_price(), buyer=request.user, ) is now running properly. Commented Jul 30, 2020 at 11:46

1 Answer 1

3

You should evaluate the timezone.now by calling the function,

date=timezone.now()
Sell.objects.create(item=item,
              date=timezone.now(),
              discount=Decimal(0.00),
              total_paid=cart.get_total_price(),
              buyer=request.user, )
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! I don't know exactly what happened but the code is now working properly without this 'timezone.now' evaluate. Btw, why this evaluation is necessary?
timezone.now is the function and timezone.now() is the result of the fucntion
In this case I'm not evaluating and the code seems to work correctly with only this lines: Sell.objects.create(item=item, date=timezone.now(), discount=Decimal(0.00), total_paid=cart.get_total_price(), buyer=request.user, ). Is it everything OK if I not add the first line 'date=timezone.now()'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.