These are chat archives for django/django

21st
Jun 2017
Anish Shrestha
@annshress
Jun 21 2017 04:09
Is there anything like Misago, Spirit like forum app, but with support for django-rest-framework?
Felipe Campelo
@fccampelo
Jun 21 2017 04:39
@annshress no
i want this too
Kyle Mulka
@mulka
Jun 21 2017 04:44
@delirious44 i assume the user has an account and they are logged in when they up vote things? Just prevent accounts from up voting things more than once. Maybe put a unique index on the model for up votes
Felipe Campelo
@fccampelo
Jun 21 2017 04:52
image.png
this my api
kind this is forenKey
Type is a foreign key in questions. Only that he is returning me the id of kind. And I wanted to bring the title! how I do ?
Felipe Campelo
@fccampelo
Jun 21 2017 04:59
@annshress thanks
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 05:34
hey everyone need help regarding learning python3..i know basics but am confused in choosing a project and stuff to learn more..mention me to talk..thanks
Felipe Campelo
@fccampelo
Jun 21 2017 05:41
@pavangudiwada do you need help or do you want to talk with projects?
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 05:42
@fccampelo i want to know how to choose and how to learn to do a project
Felipe Campelo
@fccampelo
Jun 21 2017 05:46
@pavangudiwada do you want to tolk in private?
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 05:47
@fccampelo sure if that is fine
Kyle Mulka
@mulka
Jun 21 2017 05:47
@pavangudiwada Do you want to do web stuff? Or data analysis stuff? Or something else?
Have you done Django stuff?
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 05:48
@mulka web stuff...scraping....etc..
no i dint
Kyle Mulka
@mulka
Jun 21 2017 05:50
If you want to do scraping, I recommend beautiful soup
Felipe Campelo
@fccampelo
Jun 21 2017 05:50
@mulka I am start in web and data analysis, and you ?
Miracle Ayodele
@iammiracle
Jun 21 2017 05:50
Hi, I need help on integrating Django with a payment platform, and any suggestions will be helpful
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 05:51
@mulka ohh ..ok..but how do i get started..? where do i start frm?
Miracle Ayodele
@iammiracle
Jun 21 2017 05:51
I have successfully, initialize the payment with the payment gateway - paystack
But after redirect, a new payment is initialized like its creating a new session
I don't know why, what do you suggest
Kyle Mulka
@mulka
Jun 21 2017 05:54
There's a QuickStart in the documentation for beautiful soup. You could start there: https://www.crummy.com/software/BeautifulSoup/bs4/doc/
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 05:57
@mulka thanks..can i know if i should learning anything else for this or my basics is enough?
Kyle Mulka
@mulka
Jun 21 2017 05:57
Or, if you want to make a web app, start with the Django tutorial
I'd say if you know the basic python stuff you should be good to start the Django tutorial. Lists, dictionaries, classes, functions, etc
Felipe Campelo
@fccampelo
Jun 21 2017 05:59
hey @mulka do you know about api with rest_framework
Kyle Mulka
@mulka
Jun 21 2017 06:00
Yea, I've used the Django rest framework a little bit
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 06:00
@mulka i have to learn a bit classes..
the rest i know
Felipe Campelo
@fccampelo
Jun 21 2017 06:04
@mulka i have a problem.
this is my api make with rest_framawork
Kind is foreign key. And it returns me the id. How do I get it to return the attribute name
@mulka do you help me ?
Kyle Mulka
@mulka
Jun 21 2017 06:18
I think you can do something like this:
class KindSerializer(serializers.ModelSerializer):
    class Meta:
        model = Kind
        fields = ['id', 'name']

class QuestionSerializer(serializers.ModelSerializer):
    kind = KindSerializer()

    class Meta:
        model = Question
Felipe Campelo
@fccampelo
Jun 21 2017 06:20
from rest_framework import serializers
from core.models import Question, Answer, Kind


class AnswerSerializer(serializers.ModelSerializer):

    class Meta:
        model = Answer
        fields = '__all__'


class QuestionSerializer(serializers.ModelSerializer):
    answer = AnswerSerializer(read_only=True, many=True)

    class Meta:
        model = Question
        fields = '__all__'


class KindSerializer(serializers.ModelSerializer):
    question = QuestionSerializer(read_only=True, many=True)

    class Meta:
        model = Kind
        fields = '__all__'
i make this
Kyle Mulka
@mulka
Jun 21 2017 06:21
that screenshot... is that a list of questions? what does the code look like for that?
if so, maybe you need to add kind in your QuestionSerializer
Felipe Campelo
@fccampelo
Jun 21 2017 06:24
the code this
tkanks!
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 07:16
@pavangudiwada don't use low level libraries like beautiful soup if you are newbee. Look at scrapy framework and how it organaized.
:]
@elcolie
Jun 21 2017 10:01

Hi.

import django_filters
class FactoryFilter(django_filters.FilterSet):
    shop_name = django_filters.CharFilter(name='pos__shop_name', lookup_expr='icontains')
    branch_name = django_filters.CharFilter(name='pos__branch_name', lookup_expr='icontains')
    dealer_name = django_filters.CharFilter(name='pos__dealer_name', lookup_expr='icontains')

    class Meta:
        model = Factory
        fields = [
            "id",
            "name",
            "email",
            "phone_number",
            "fax_number",
            "touch_capacity",
            "car_capacity",
            "mechanic_capacity",
        ]

I want to add .distinct() the the end of the queryset. How can I do that?

class FactoryListPOSViewSet(viewsets.ModelViewSet):
    """
    iOS POS App will query and use filter on this ViewSet
    Although it is similar to `FactoryViewSet`. I decided to split because of functionality
    """
    queryset = Factory.objects.all()
    serializer_class = FactorySerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_class = FactoryFilter
    permission_classes = []
    authentication_classes = []
from django.db import models
from django.db.models import CASCADE

from factory.apps.factories.models import Factory


class FactoryPOS(models.Model):
    """
    A relational table between POS and Factory
    """
    # max_length=255 follow the eneos-pos-web
    branch_name = models.CharField(max_length=255)
    dealer_name = models.CharField(max_length=255)
    shop_name = models.CharField(max_length=255)
    factory = models.ForeignKey(Factory, related_name='pos', on_delete=CASCADE)

    # Delay property.
    drop_off = models.PositiveSmallIntegerField(default=0)
    pickup = models.PositiveSmallIntegerField(default=0)

    class Meta:
        unique_together = ('branch_name', 'dealer_name', 'shop_name', 'factory')

    def __str__(self):
        return self.branch_name
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 10:08
@elcolie you can overwrite filter_queryset on view and call distinct after super call (which must return QuerySet after all filter backends applied). See https://github.com/encode/django-rest-framework/blob/master/rest_framework/generics.py#L142
:]
@elcolie
Jun 21 2017 10:08
One moment
@romantolkachyov Thank you very much :+1:
I though I have to change class and modify the serializer
Job Matheka
@jobm
Jun 21 2017 11:36
is it valid to do this:
x = "Yes"
if x == "Yes" or None:
    print("do something")
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 11:54

@jobm if x was from user input, better use something like this:

def str2bool(v):
  return v.lower() in ("yes", "true", "t", "1")
if str2bool(x): pass

And or None part not necessary.

Job Matheka
@jobm
Jun 21 2017 11:55
@romantolkachyov I See the sense in that Thanks, was thinking of a similar thing. :)
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 11:56
@romantolkachyov can you tell me more about it?
Job Matheka
@jobm
Jun 21 2017 11:59
Is anyone here familiar or experienced in oTree??
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 12:11
@pavangudiwada it has good docs https://scrapy.org
hardik Gajjar
@gajjar8055
Jun 21 2017 13:10
truncated or oversized response headers django WCGI
Apache2 with WSGI
Django 1.10
Ubuntu 16.04 AWS server
Anyone for help
:]
@elcolie
Jun 21 2017 13:21
How many people has netmiko experience?
Ghost
@ghost~58a30cbed73408ce4f4a4c2a
Jun 21 2017 14:07
@romantolkachyov thanks ill try it :)
Anish Shrestha
@annshress
Jun 21 2017 16:15
In django-rest-framework's ModelViewSet what is the best way to update request.data.
For example, I have been doing:
request.data.update(buyer=request.user) which is obviously not the best practice...
@romantolkachyov ?
Anyone?
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 16:17
@annshress why you need this?
Anish Shrestha
@annshress
Jun 21 2017 16:19
Because, I can;t trust data from front end. My model has fields that should not be coming from the front end, rather back end should be changing the incoming data.
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 16:24
@annshress You can overwrite views create / update methods https://github.com/encode/django-rest-framework/blob/master/rest_framework/mixins.py#L18
Аббасов Александр
@abbasovalex
Jun 21 2017 16:25
@annshress your question about update user only or wide?
Anish Shrestha
@annshress
Jun 21 2017 16:26
User and few other fields
currently, I am doing that way @romantolkachyov
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 16:29
@annshress The better way is to pass request.user to serializer context and overwrite serializer methods (create/update). But don't rely on defaults request context because it would be hard to test it. It is always better to place model logic in serializer and buisness logic in view (it is a controller, you know :-) )
Anish Shrestha
@annshress
Jun 21 2017 16:30
Hmm, Ok, will make changes for that.
And, I am having hard time, passing extra kwargs: store in the urlpost_list to the viewset. I am doing it right?
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 16:33
@annshress think this way: if you need to change view when serializer structure changed — something goes wrong
Anish Shrestha
@annshress
Jun 21 2017 16:34
:laughing: True Shit
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 16:37
@annshress you can add store argument to the method (or change group name in urlpatterns from store to store_id and add argument with store_id name to list method) — this way you don't need to pop it from kwargs.
Anish Shrestha
@annshress
Jun 21 2017 17:15
anyway, like you said, i am refactoring code logic to the serializers
But i am not able to patch request.
i am passing pk kwarg. But somehow, i end up with store = <pk-value> kwarg in the viewset.
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 17:26
url(r'^post/(?P<store>[0-9]+)/$', post_list, name='post-list'),
url(r'^post/(?P<pk>[0-9]+)/$', post_detail, name='post-detail'),
Not a good idea.
You've missed store in second line @annshress
Anish Shrestha
@annshress
Jun 21 2017 17:29
Yes, giving it a try. Has not been documented. I think will change that. and instead take store from post data , most probably.
Is that the only way?
Roman Tolkachyov
@romantolkachyov
Jun 21 2017 17:31
Two urls matching the same pattern. You can choose any other, like:
url(r'^post/(?P<store>[0-9]+)/$', post_list, name='post-list'),
url(r'^post/detail/(?P<pk>[0-9]+)/$', post_detail, name='post-detail'),
delirious44
@delirious44
Jun 21 2017 17:50
@mulka no, there's no user thing .. you just enter the website and play.. smash or pass .. if u smash, u give an upvote
Kyle Mulka
@mulka
Jun 21 2017 17:53
You could limit up votes by IP address.
delirious44
@delirious44
Jun 21 2017 17:54
@mulka That feels hacky .. is there any other way to make like just one click
or something like that
or maybe not be able to just write the url
and get the upvote
maybe something different than url .. is there a way to modify the db through js or something ?
Kyle Mulka
@mulka
Jun 21 2017 17:56
You could put a unique value in the HTML and send that along. You could prevent that unique value from being used more than once
You could use cookies. Limit up votes by user session.
delirious44
@delirious44
Jun 21 2017 17:57
ive tried to put a random 7 digit value along the user id
but you can write whatever 7 digit value and it will be accepted
even if it isnt the same as the one in the form action
like
12345567<id>/up
and if the user writes
7654322<id>/up .. it will work
how do u put the unique value in the html ?
Kyle Mulka
@mulka
Jun 21 2017 18:03
Like, when the page is rendered, generate a unique value and store it in the database. Then, when submitting the upvote, either remove the unique value from the database or just check that it hasn't been used before to upvote
Are you doing an Ajax post?
delirious44
@delirious44
Jun 21 2017 18:13
no im not doing an ajax post
ive found little to no use of javascript
while working with django
idk why
the way im thinking of REST API
is similar to a normal API , you just get JSON data and thats it
since im able to render django db data directly from views.py and put them in html file through context dictionary
i dont see why i would need js for anything or a rest api for that matter
unless rest API has other things going for it
Kyle Mulka
@mulka
Jun 21 2017 18:36
If you are on a page and want to basically stay on that page but still cast a vote without reloading the page, that's a good use case for Ajax.
Most APIs these days are REST APIs (or similar) anyways
Most use JSON. Some use XML
Anish Shrestha
@annshress
Jun 21 2017 18:45
@romantolkachyov Oh Yes! Now I feel like total ass. :worried:
Thanks for pointing that out...
Miracle Ayodele
@iammiracle
Jun 21 2017 21:00
i asked about payment platform and django on this platform and no one could help
Hassen ben tanfous
@HassenPy
Jun 21 2017 21:18
hey @koderNeptune , what payment platform?
Miracle Ayodele
@iammiracle
Jun 21 2017 21:48
paystack
@HassenPy, i have successfully integrated it, but i need to perform some validation on it
so i need to validate
Hassen ben tanfous
@HassenPy
Jun 21 2017 21:51
that's a dummy transaction right?
i mean the data in the url is dummy data?
if not delete it
from the chat
Miracle Ayodele
@iammiracle
Jun 21 2017 21:53
its dummy
i cant post live here
Hassen ben tanfous
@HassenPy
Jun 21 2017 21:53
hmm, no experience with paystack, try posting a detailed question on stackoverflow and paste link here so that other people can help
srry m8
Miracle Ayodele
@iammiracle
Jun 21 2017 21:54
okay
no problem, thanks for the attention
Hassen ben tanfous
@HassenPy
Jun 21 2017 21:55
mention me when you paste link
Miracle Ayodele
@iammiracle
Jun 21 2017 21:55
ok