Anique Akhtar
Anique Akhtar

Reputation: 195

Django: How to create Table and then update Database through them

I am new at Django and I have tried my best to understand it but I still have a long way to go. I am working on a project in which I have to make a webpage where I have to display data from these classes :

class team(models.Model):
    team_name = models.CharField(max_length = 40)
    def __unicode__(self):
            return self.team_name

class metric(models.Model):
    team = models.ForeignKey(team)
    metric_name = models.CharField(max_length = 40)
    def __unicode__(self):
            return self.metric_name


class members(models.Model):
    metric = models.ForeignKey(metric)
    member_ID = models.CharField(max_length = 40)
    member_name = models.CharField(max_length = 40, null=True, blank=True)
    def __unicode__(self):
            return self.member_ID

Furthermore I want this data to be editable as well. Meaning there should be an Edit button which once pressed would enable the user to edit these fields and once he saves it the database get's updated automatically.

Now as far as I have understood. I would need to display this data using tables of some sort and then once the user clicks on the Edit button I would have to change the Tables into form so he can edit the data ? and then update the database once he clicks on save.

I am not sure how to do all this in Django. If anyone could please refer to me step by step on how to proceed and maybe refer to some References It would help me a lot

Thanks :)

UPDATE: I forgot to mention it before. I have already created a page where user selects team_name.. Then he is redirected to another page where I want to show the table for that specific team. And I also want that page to be editable. If I use modelforms then I won't be able to access single object of my team model.

UPDATE2: I am still stuck at this. I can't understand on how to display just few elements and variables of a model class rather then all of it. Apart from that I can't understand how to create a Form that can take input from the user on which object of the database to edit and then display that certain objects to be editable.

Upvotes: 4

Views: 9751

Answers (2)

Thomas Schwärzl
Thomas Schwärzl

Reputation: 9917

Related to the questioneers-comments here and here. First of all, you should write your class-names capitalized. Please check the documentation.

You create Forms for a queryset like this.

forms.py

from django.forms import ModelForm
class MembersForm(ModelForm):
    class Meta:
        model = Members

urls.py

from django.conf.urls.defaults import *

urlpatterns = patterns('yourapp.views',
    url(r'^$',
        # /
        view = 'index',
        name = 'team-index',
    ),
    url(r'^(?P<lookup_team>[-\w]+)/$',
        # .../team-extreme/
        view = 'update',
        name = 'update-members',
    ),
)

views.py (of your application)

from django.http import HttpResponse
from django.template.context import RequestContext
from django.shortcuts import render_to_response, reverse, redirect
from yourapp.models import Members
from yourapp.forms import MembersForm

def index(request):
    queryset = Members.objects.all()

    template = 'yourapp/index.html'
    kwvars = {
        'data': queryset,
    }
    return render_to_response(template, kwvars, RequestContext(request))


def update(request, lookup_team):
    """change members being in a special team"""

    queryset = Members.objects.filter(metric__team__team_name=lookup_team)

    if request.POST:
        form = MemberForm(request.POST, instance=queryset)
        if form.is_valid():
            form.save()
            return redirect(reverse('team-index'))
    else:
        form = MemberForm(instance=queryset)

    template = 'yourapp/update.html'
    kwvars = {
        'form': form,
    }
    return render_to_response(template, kwvars, RequestContext(request))

Hope this helps. If you got any questions hit me a comment.

Upvotes: 4

A.J.Rouvoet
A.J.Rouvoet

Reputation: 1213

If you want to implement it as dynamically as you describe, then this is impossible by Django alone; Django can serve you the table and the forms, but a dynamic change without reloading the page (which I'm guessing is what you really want) needs some client side code, JavaScript.

But about showing the data and the forms: use Django's class based generic views.

The ListView and DetailsView should get you started with showing the data from the models.

The ModelForm in combination with the appropriate generic editing views should get you going with editing the data.

These views will, per default, return the data in some - generally useful - format and can be customized by using your own template. I suggest you read up on the links I provided you and work from there. That should get you going.

Upvotes: 1

Related Questions