3

I have a form to add new data to my database, but this form overwrites the existing data in my table. I want to add a new tuple and keep old tuples in my database's table

forms.py

class StudentForm(forms.ModelForm):
    class Meta:
        model = student
        fields = ('First_Name', 'Last_Name', 'Birthday', 'Phone', 'Mobile', 'STNO',
          'Father_Name', 'Father_Job', 'Father_Phone', 'ID_Code',
          'National_ID', 'Address', 'Study_Field', 'Probation')

views.py

def add_student(request):
    if request.method == "POST":
        form = StudentForm(request.POST)
        if form.is_valid():
            new_student = form.save(commit=True)
            new_student.author = request.user
            new_student.save()
            return redirect('../')
    else:
        form = StudentForm()
    return render(request, 'school_manager/students/new_student.html', {'form': form})

models.py

class student(models.Model):
    id = models.IntegerField(default=1,null=False,primary_key=True)
    First_Name = models.CharField("First Name ", max_length=100,null=True)
    Last_Name = models.CharField("Last Name ",max_length=100,null=True)
    Birthday = models.CharField("Birthday ",max_length=10,null=True)
    Phone = models.CharField("Phone ",max_length=20,null=True)
    Mobile = models.CharField("Mobile ",max_length=20,null=True)
    STNO = models.CharField("STNO ",max_length=10,null=True)
    Father_Name = models.CharField("Father Name ",max_length=100,null=True)
    Father_Job = models.CharField("Father Job ",max_length=100,null=True)
    Father_Phone = models.CharField("Father Phone ",max_length=20,null=True)
    ID_Code = models.CharField("ID Code ",max_length=10,null=True)
    National_ID = models.CharField("National ID ",max_length=10,null=True)
    Address = models.CharField("Address ",max_length=200,null=True)
    Study_Field = models.CharField("Study Field ",max_length=100,null=True)
    Probation = models.BooleanField("Probation ",default=True)
    def __STR__ (self):
        return self.STNO
6
  • What table? Do you mean you don't want to update new_student and instead create a new one? Commented Dec 11, 2015 at 9:30
  • database table ..... yes i want to create but this code update existing row @Sayse Commented Dec 11, 2015 at 9:42
  • Please show also your student model. Commented Dec 11, 2015 at 9:43
  • Possible duplicate of How do I clone a Django model instance object and save it to the database? Commented Dec 11, 2015 at 9:48
  • i just want add a new table row :( . for example a blog ..... add a new post with title and content when click "Add" button each time @Sayse Commented Dec 11, 2015 at 9:54

2 Answers 2

4

The model was relevant. Look this:

class student(models.Model):
    id = models.IntegerField(default=1,null=False,primary_key=True)

You are always creating student number 1.

You should to set a new id by hand for each student:

def add_student(request):
    if request.method == "POST":
        form = StudentForm(request.POST)
        if form.is_valid():
            new_student = form.save(commit=False)
            id = a_function_to_take_another_id_for_a_student()
            new_student.id = id
            new_student = form.save(commit=True)
            new_student.author = request.user

Another approach is to can change type id to auto increment field:

id = models.AutoField(primary_key=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Good spot, I rescind my comment.. (The model shouldn't have been relevant :P)
2

Problem is in your model. You've explictly set ID field, as below:

    id = models.IntegerField(default=1,null=False,primary_key=True)

But you're not setting it in form. That way django will each time create row with ID = 1 (because that's default value).

When you're creating field with ID that already exists in database, django will update existing field because it can't create new (there can't be more than one field with same primary key).

You should change type of ID to AutoField or remove it's definition, django will then create default ID field that will be an AutoField.

2 Comments

i used that "default=1" in the past for some problems and i forget remove that .... thank you ... problem solved
@ArashHatami, you know how to say thanks: vote up their post ! Every upvote is an implicit "thank you".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.