Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save data from a ModelForm to database in django?

Tags:

python

django

I have a model:

class Cost(models.Model):
    project = models.ForeignKey(Project)
    cost = models.FloatField()
    date = models.DateField()

For the model I created a ModelForm class:

class CostForm(ModelForm):
    class Meta:
        model = Cost
        fields = ['date', 'cost']

with view.py:

def cost(request, offset):
    if request.method == 'POST':
        # NOTE: How to save the data in DB?
        return HttpResponseRedirect('/')
    else:
        form = CostForm()

and next template:

<form action="/cost/{{ project }}/" method="post" accept-charset="utf-8">
    <label for="date">Date:</label><input type="text" name="date" value={{ current_date }} id="date" />
    <label for="cost">Cost:</label><input type="text" name="cost" value="0" id="cost" />
    <p><input type="submit" value="Add"></p>
</form>

Question

How I can save the data from the form to the database?

Some additional information:

model.py contains

class Project(models.Model):
    title = models.CharField(max_length=150)
    url = models.URLField()
    manager = models.ForeignKey(User)
    timestamp = models.DateTimeField()

My attempt

I tried to implement the solution in a next way (note: offset = project name):

def cost(request, offset):
    if request.method == 'POST':
        form = CostForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.project = Project.objects.filter(title=offset)
            instance.date = request.date
            instance.cost = request.cost
            instance.save()
        return HttpResponseRedirect('/')
    else:
        form = CostForm()

But it does not work :(

like image 396
akrisanov Avatar asked May 14 '10 20:05

akrisanov


People also ask

Which method is used to save a model in DB in Python Django?

Saving objects. To save an object back to the database, call save() : Model.

How do you receive data from a Django form with a post request?

Using Form in a View In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin.

What is Save () in Django?

save() , django will save the current object state to record. So if some changes happens between get() and save() by some other process, then those changes will be lost.


3 Answers

A couple things don't look right:

  1. Project.objects.filter() will return a queryset. Use Project.objects.get() instead... it will return just a single Project object

  2. You wont need to explicitly set the cost and date, that will be handled by your instance=form.save(commit=False)

  3. You aren't using the form in your template...

Try this:

Template:

<form action="/cost/{{ project }}/" method="post" accept-charset="utf-8">
     {{form.as_p}}
     <p><input type="submit" value="Add"></p>
</form>

View:

def cost(request, offset):
    if request.method == 'POST':
        form = CostForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.project = Project.objects.get(title=offset)
            instance.save()
            return HttpResponseRedirect('/')
    else:
        form = CostForm()

    render_to_response('path/to/template.html',{'form':form},context_instance=RequestContext(request))

Also, I think you will need to add blank=True to your models.ForeignKey(Project) in the Cost model. That will allow your ModelForm to validate.

like image 92
Brant Avatar answered Sep 21 '22 23:09

Brant


I found the solution. Rewrote view.py as follows:

def cost(request, offset):
    if request.method == 'POST':
        project  = Project.objects.get(title=offset)
        date     = request.POST.get('date', '')
        cost     = request.POST.get('cost', '')
        cost_obj = Cost(project=project, date=date, cost=cost)
        cost_obj.save()
        return HttpResponseRedirect('/')

The rest of the code has not changed.

like image 34
akrisanov Avatar answered Sep 21 '22 23:09

akrisanov


I did it this way:

def save_model(self, request, obj, form, change):
    obj.save()

To modify something, do it through:

obj.xyz = 'something'
obj.save()
like image 37
Rahul Avatar answered Sep 22 '22 23:09

Rahul