partial table dump using mysqldump

There are times when you only want to take partial dump of a mysql table. You can use mysqldump command with one extra option “–where”. You will have to provide your where clause (partial dump condition) in the “–” where option.

Below is the command to take a partial dump of a table.

mysqldump -u [username] -p  [databasename] [mytablename] --where  " mycolumn = 'somevalue' " > filename.sql

Related Posts:
1. MySQL dump in XML format.
2. MySQL general query logs.

Developer gets honest

 

Install Go 1.8 on Fedora / Ubuntu

You can follow below steps to install Go 1.8 on Fedora / Ubuntu. (It should work for other Linux distribution as well but it is only tested for Ubuntu / Fedora).

Step 1:

Download the Go 1.8 bundle by clicking here.

Step 2:

Extract the downloaded bundle using below command,

tar -C /usr/local -xzf go1.8.linux-amd64.tar.gz 

Step 3:

Add below to your to your path variable, you can add this to either of bash_profile or /etc/profile.

export PATH=$PATH:/usr/local/go/bin

Reload the configuration & GO will be available to use.

 

Mysql Dump in XML format

To take dump in XML format you have to provide an extra option in default mysqldump command. This can be useful in cases where you want to migrate your MySQL database to some other database.

You can use below command to take MySQL Database dump in XML format.


mysqldump --xml -u[username] -p[password] [db_name] > filename.xml

or


mysqldump -X -u[username] -p[password] [db_name] > filename.xml

For example if you have a database test_database with an username as root and password as mypassword the command to take dump will be,


mysqldump --xml -u root -p mypassword test_database > filename.xml

Handling two Django models in a single POST

There are times when, you want to handle multiple Django models in a single POST request (By this i meant data posted from html form/ template contains data of two different Django models). I was struggling with this few months back and got a fantastic reference link from #django on IRC.

You can use the below method as a simple example, here’s a view that updates a User and their Profile in one view:

def user_edit(request):
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(request.POST, instance=request.user.profile)

        if all([user_form.is_valid(), profile_form.is_valid()]):
            user = user_form.save()
            profile = profile_form.save()
            return redirect(user)
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)

    return render(request, 'user_form.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            })

The two most important things to notice here are:

a. The use of all() instead of user_form.is_valid() and profile_form.is_valid().

This is because Python will skip the if clause if the first form is not valid, and so it won’t run validation on the second form. If some error is present in second from user will again get the error second time. using all() runs validation on all the submitted forms thus you get the errors at one time.

b. Two forms in the context.

Just render them all inside the one <form> tag, and it will be fine. If any of the field names clash, pass a prefix= when constructing one of the forms, to prefix its field names so they’re unique.

Related blogs :

  1. Django – AttributeError: ‘Http404’ object has no attribute ‘status_code’
  2. Make primary key with two or more field in Django