jeudi 2 juin 2016

Django : RunTimeWarning : DateTimeField received a naive datetime while time zone support is active

I am trying to test a cart creation based on django cart : http://ift.tt/24lULJm

But I have this error when I am trying to create the cart :

RunTimeWarning : DateTimeField received a naive datetime while time zone support is active

I did some research but I couldn't resolve my problem for datetime.datetime.now()

test_views.py in my tests directory :

from django.test import TestCase, Client, RequestFactory import unittest from django.contrib.auth.models import User, AnonymousUser from front.models import Entreprise, Cart, CartItems from decimal import Decimal from front.cart import models import datetime import pytz from pytz import all_timezones from django.utils import timezone

class CartAndItemModelsTestCase(TestCase):

    def setUp(self):
        self.client = Client()
        self.request = RequestFactory()
        self.request.user = AnonymousUser()
        self.request.session = {}

    def _create_cart_in_database(self, creationDate=datetime.datetime.now(), checkedOutDate=True):
        """
            Helper function so I don't repeat myself
        """
        cart = models.Cart()
        cart.creationDate = creationDate
        cart.checkedOutDate = False
        cart.save()
        return cart

    def _create_item_in_database(self, cartId, productId, quantity=1, unit_price=Decimal("100")):
        """
            Helper function so I don't repeat myself
        """
        CartItems = CartItems()
        CartItems.cartId = cartId
        CartItems.productId = productId
        CartItems.quantity = quantity
        CartItems.unit_price = unit_price
        CartItems.save()

        return CartItems

    def _create_user_in_database(self):
        """
            Helper function so I don't repeat myself
        """
        user = User(username="user_for_sell", password="sold",
                email="example@example.com")
        user.save()
        return user

    def test_cart_creation(self):
        creationDate = datetime.datetime.now()
        cart = self._create_cart_in_database(creationDate)
        id = cart.id

        cart_from_database = models.Cart.objects.get(pk=id)
        self.assertEquals(cart, cart_from_database)

models.py :

class Cart(models.Model):
    creationDate = models.DateTimeField()
    checkedOutDate = models.DateTimeField(null=True)
    userId = models.ForeignKey('auth.User', null=True)
    class Meta:
        ordering = ['creationDate']

    def userType(self):
        user = MyUserClass(self.userId.email)
        return user.type
    userType = property(userType)

class CartItems(models.Model):
    cartId = models.ForeignKey('Cart')
    productId = models.ForeignKey('Products')
    entrepriseId = models.ForeignKey('Entreprise', null=True)
    quantity =  models.PositiveIntegerField()
    unitPrice = models.DecimalField(max_digits=18, decimal_places=2)
    unit = models.CharField(max_length=50, null=True)

    def totalPrice(self):
        return self.quantity * self.unitPrice
    totalPrice = property(totalPrice)

I also have USE_TZ = True in my settings.

Thanks in advance for your help !

Aucun commentaire:

Enregistrer un commentaire