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
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
  • What table? Do you mean you don't want to update `new_student` and instead create a new one? – Sayse Dec 11 '15 at 09:30
  • database table ..... yes i want to create but this code update existing row @Sayse – Arash Hatami Dec 11 '15 at 09:42
  • Please show also your `student` model. – GwynBleidD Dec 11 '15 at 09:43
  • Possible duplicate of [How do I clone a Django model instance object and save it to the database?](http://stackoverflow.com/questions/4733609/how-do-i-clone-a-django-model-instance-object-and-save-it-to-the-database) – Sayse Dec 11 '15 at 09: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 – Arash Hatami Dec 11 '15 at 09:54
  • ArashHatami - Thats a different question, I was assuming you're updating a model, otherwise it should have no problem creating a new one, (unless something strange is going on with the `commit` parameter, this should be `False`) – Sayse Dec 11 '15 at 09:57

2 Answers2

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)
dani herrera
  • 48,760
  • 8
  • 117
  • 177
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.

GwynBleidD
  • 20,081
  • 5
  • 46
  • 77
  • i used that "default=1" in the past for some problems and i forget remove that .... thank you ... problem solved – Arash Hatami Dec 11 '15 at 10:06
  • @ArashHatami, you know how to say thanks: [vote up their post ! Every upvote is an implicit "thank you".](https://blog.stackoverflow.com/2011/01/how-to-say-thanks-in-an-answer/) – dani herrera Dec 11 '15 at 10:35