Tuesday, 21 August 2018

Django rest api - filter

views.py

from django_filters.rest_framework import DjangoFilterBackend

class PollList(viewsets.ModelViewSet):
    queryset = X.objects.all()
    serializer_class = XSerializer
    filter_backends = (DjangoFilterBackend,)
    filter_fields = ('xxxx',)

Multiple model serializer single api - djnago rest framework

views.py
from drf_multiple_model.views import ObjectMultipleModelAPIView

class TextAPIView(ObjectMultipleModelAPIView):
    def get_querylist(self):
        querylist = (
        {'queryset': Educations.objects.all(), 'serializer_class':
        EducationListSerializer},
        {'queryset': Citys.objects.all(), 'serializer_class':
        CityListSerializer},
        {'queryset': Languages.objects.all(), 'serializer_class':
        LanguageListSerializer},
        {'queryset': Professions.objects.all(), 'serializer_class':
        ProfessionListSerializer},
        )
        return querylist


urls.py

from rest_framework import viewsets

url(r'^data', TextAPIView.as_view()),

Pubnub Integration using JS

 <script src="https://cdn.pubnub.com/sdk/javascript/pubnub.4.21.5.js">  </script>

<script type="text/javascript">
    console.log('testtt');
    var pubnub = new PubNub({
    subscribeKey: "demo",
    publishKey: "demo",
    ssl: true
})

    pubnub.subscribe({
        channels: ['mahesh']
    });

pubnub.publish({
        channel: 'mahesh',
        message: {
          sender_name: 'Langote Mahesh',
          message: "text,
          date_time: "13 jan 2018",
        }
      });

    pubnub.history(
        {
            channel: 'mahesh',
            reverse: true,
            count: 100
        },
        function (status, response) {
             var data = response.messages;
            $.each( data, function( key, value ) {
              $.each( value, function( key, value ) {
                    if (key == "entry") {
                        console.log(key + " : "+ value);
                            console.log(value.sender_name);
                            console.log(value.message);
                            console.log(value.date_time);
                    }
                });
            });
           
        }
);
 
   
       
       

How to add Padding or Margin on input fiend from backend or django form

form.py

text = forms.CharField(required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'text', 'name':'answers', 'autocomplete': 'off', 'style': 'width: 80% !important; resize: vertical !important;margin-top:6px;margin-left:98px;'}))

Wednesday, 25 July 2018

How to count no of child create on parent model??

Step 1 : make child mode with apply Foreign Key on child model with parent model
ex:
first model
class Poll(models.Model):
    poll_id = models.AutoField(primary_key=True)

second model
class Questions(models.Model):
    poll = models.ForeignKey(Poll, models.DO_NOTHING, db_column='poll', blank=True, null=True, related_name="poll")

In poll template:

{{ p.poll.count }}

Friday, 20 July 2018

How to call api in client side with filter

<button type="submit" onclick="UserAction()">Search</button>


unction UserAction() {
           
           $.ajax({
               type: 'GET',
               url: 'http://127.0.0.1:8000/api/project/?industry_sector=Automobiles&location=State',
               success: function(res){
                       console.log(res)
                       $.ajaxSetup({
                          headers: {
                            "token": res.token
                          }
                       });
               },
               error: function(error) {
                   callbackErr(error,self)
               }
           })
      }

Django rest api - filter

views.py from django_filters.rest_framework import DjangoFilterBackend class PollList(viewsets.ModelViewSet):     queryset = X.objects...