Jaydeep
Jaydeep

Reputation: 803

How to provide "add" option to a field in django admin?

In django admin i had one model named 'student', here it has three fields, NAME, ROLL NO. & SUBJECT. in models.py:

from django.db import models

class Student(models.Model):
    Name = models.CharField(max_length=255, blank = False)
    Roll_No = models.CharField(max_length=10, blank = False)
    Subject = models.CharField(max_length=20, blank = False)

    def __str__(self):
        return self.Name

now i want this SUBJECT field to be Dynamic, like there will be "+" sign besides the SUBJECT field, by clicking that one more SUBJECT field will be added right after it and so on, but maximum 10 SUBJECT fields can be added like that.

Upvotes: 0

Views: 112

Answers (1)

ParthS007
ParthS007

Reputation: 2689

You can update the existing value when doing the subject addition from views.

Like for example in views:

student = Student.objects.get(id=1)

student.Subject += "New_Subject_Name" 

student.save() # this will update only

Add other conditions/check for the existing subject and check for 10 subjects accordingly.

Upvotes: 1

Related Questions