class MyForm(forms.ModelForm):
    OPTIONS = (
        ("IN", "India"),
        ("US", "United States"),
          )
    countries = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, label = "Select Medications", choices=OPTIONS)
#To change the order use the below code
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['name', 'countries', 'states']

    class Meta:
            model = MyModel

 

Python - Convert list to String

Posted In: . By CreativeSolutions


 First, if it is a list of strings, you may simply use join this way:

 >>> mylist = ['elegant', 'kerala', 'package']
 >>> print ', '.join(mylist)
     Then the output will be as in below. If you need to print in separate line you can use the '\n' instead of  ', '
     elegant, kerala, package

 

How to use group by clause to fetch related data

Posted In: . By CreativeSolutions


There is no exact group by clause and we can use the function annotate to get the count of records with grouping data.

   If we need to fetch the related data from 2 tables,
    first we need to fetch the unique data from first table
    then we can use the filter function with id__in

  ex:    
    mapdata = MedicationMapping.objects.all()
    meds = Medication.objects.filter(id__in=mapdata)

 


LOAD DATA LOCAL INFILE 'file-path/file_name.csv' INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'

For more details
  http://dev.mysql.com/doc/refman/5.1/en/load-data.html



 

How to do add pagination in django framework

Posted In: . By Webdevelopmentlogics


In views.py add the below code
  d = FlatPage.objects.all() // Flatpage is the Model class
    paginator = Paginator(d, 2) # Show 2 help on each row
    page = request.GET.get('page')
   
    try:
        helps = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        helps = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        helps = paginator.page(paginator.num_pages)
    return render_to_response("flatpages_plus/list.html",{
            "data": helps }, RequestContext(request),
        )

In the template add teh following code

 div class="pagination"
         

                {% if data.has_previous %}
                 
  • previous

  •             {% endif %}
                {% if data.page_range_data.show_first %}
                 
  • 1

  •             {% endif %}
                {% for i in data.page_range_data.page_range %}
                    {% ifequal i data.number %}
                        {{ i }}
                    {% else %}
                       
  • {{ i }}

  •                 {% endifequal %}
                {% endfor %}
                {% if data.page_range_data.show_last %}
                   
  • {{ data.paginator.num_pages }}

  •             {% endif %}
                {% if data.has_next %}
                   
  • next

  •             {% endif %}
               

        /div