These are chat archives for django/django

20th
Jun 2018
djangoReactGuy
@SanskarSans
Jun 20 2018 07:38

I am having problem in adding the product to the cart. Can anyone help me at this, please?

https://stackoverflow.com/questions/50938691/add-to-cart-using-django-rest-framework

Luke-VideoLibre
@LukeVideo
Jun 20 2018 07:50
Hy if i use gunicorn i don't need STATIC_ROOT if i want to collect statics i have to add it then i might have to comment it and i kinda have enough of editing my settings.py. Specialy that when i pull a new branch it might or might not have it commented the right way round according to what i did or fixed... Does anyone have a solution ? or a pro tip to handle this.
:]
@elcolie
Jun 20 2018 08:44
@SanskarSans https://github.com/elcolie/zero-to-deploy
Check my repository. I use write_only with nested serializer for this sort of job.
https://github.com/elcolie/zero-to-deploy/blob/master/orders/tests.py is my endpoint usecase
djangoReactGuy
@SanskarSans
Jun 20 2018 09:38
Do I have to create different serializer?
:]
@elcolie
Jun 20 2018 09:38
@SanskarSans Yes. The idea of nested is that
djangoReactGuy
@SanskarSans
Jun 20 2018 09:44
so i get the product with write_only=True, and quantity only with the new serializer.
Based on the model I have, it should be like this then? I get confuse with which model i use in serializer. Cart or CartItem.

class Cart(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.CASCADE)
    items = models.ManyToManyField(Cycle, through='CartItem')
    subtotal = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
    tax_percentage = models.DecimalField(max_digits=10, decimal_places=5, default=0.085)
    tax_total = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
    total = models.DecimalField(max_digits=50, decimal_places=2, default=0.00)
    objects = CartManager()

    def __str__(self):
        return str(self.id)


class AddToCartSerializer(serializers.ModelSerializer):
  items = serializers.PrimaryKeyRelatedField(queryset=Product.objects.all(), many=True, write_only=True)
  class Meta:
    model = Cart
    fields = ['items', 'quantity']
Luke-VideoLibre
@LukeVideo
Jun 20 2018 09:58

@ArtistsTechGuy @funkybob sorry i totally missed your thread.
I'm going for

# Set ENV_DEBUG to "True" in .env if you need Django Debug.
DEBUG = False
if os.environ['ENV_DEBUG'] == "True" : 
    DEBUG = True

For readability and because it defaults to False. And i add the double check for "True" as i don't want a false positive with a "False" that could trigger True...
And i added a comment for me in the futur... The most important here isn't being the most Pythonic but being sure debug isn't accidently on in production.

Curtis Maloney
@funkybob
Jun 20 2018 10:48
@LukeVideo you might consider using:
 os.getenv('ENV_DEBUG', '').lower() = 'true'
Luke-VideoLibre
@LukeVideo
Jun 20 2018 11:57
@funkybob indeed ! Also I didn't check if no ENV_DEBUG break's settings.py.
Pete Tinkler
@ptink
Jun 20 2018 11:57
if you're looking for a more permanent solution then django-environ is good
Luke-VideoLibre
@LukeVideo
Jun 20 2018 12:06
I'll check it out ! Thanks.
:]
@elcolie
Jun 20 2018 12:12
@SanskarSans Use CartSerializer and in it. It has CartItemSerializer inside it
@SanskarSans Your model is hard to work with. You'd better use models.FK not models.ManyToMany
Since you can use related_name and related_query_name help you do reverse relation later
Curtis Maloney
@funkybob
Jun 20 2018 13:01
@LukeVideo if you're open to 3rd party tools.... my lib django-classy-settings is quite popular, it seems
Luke-VideoLibre
@LukeVideo
Jun 20 2018 13:08
Thanks, looks good.
djangoReactGuy
@SanskarSans
Jun 20 2018 14:14
@elcolie can you help me by simplifying the code, please?
not in detail but a simple gist