开发者

problem with accesing multiple values using django

I am new to python and I am using django for student database application . Student database application must show id, firstname,lastname,subjectnames,marks. Single student is having multiple subjects and their marks. I am getting problem with accessing multiple values that student is having multiple subjects and marks.

models.py

class Person(models.Model):
    firstname=models.CharField(max_length=50)
    lastname=models.CharField(max_length=50)

    def __unicode__(self):
        return (self.firstname,self.lastname)


class Marksheet(models.Model):
    subname=models.CharField(max_length=50)
    marks=models.IntegerField(max_length=10)
    person=models.ForeignKey(Person)

    def __unicode__(self):
        return self.subname

views.py

def add_page(request,page_name): # function for creating  the new records
    p1=None
    p2=None
    if request.method=='POST':
        p1=Person(firstname=request.POST['firstname'],lastname=request.POST['lastname'])
        p1.save()
        p2=Marksheet(subname=request.POST.getlist('subnames'),person=Person(person_id))
        p2.save()
    return render_to_response("add.html",{"page_name":page_name})

creating a records I am using form in html which i开发者_StackOverflows shown below....

Templates add.html

<form method="post" action="/newdcl/{{page_name}}/add/" > {% csrf_token %}
    First name:  <input type="text" name="firstname" /> <br />
    Last name:  <input type="text" name="lastname" /> <br /> 
    Operating System  <input value="os" name="subnames" type="checkbox"><br />
    System Programming <input value="sp" name="subnames" type="checkbox"> <br />
    Maths <input value="maths" name="subnames" type="checkbox"> <br />        
    <input type="submit" value="save" >
</form> 

Can anyone help me in this????


Your problem seems to lie in your how you try to create Marksheet, you can't assign a list of values to one field like that. Using your currently formless, scary, no-validation, setup... you can do something like this-

p1=Person(firstname=request.POST['firstname'], 
          lastname=request.POST['lastname'])
p1.save()
for subname in request.POST.getlist('subnames'):
    new = MarkSheet(subname=subname, person=p1) 
        #no data for marks, must define it to be able to be blank/null
    new.save()

You will need to add blank=True, null=True to you marks field in your models.py if you intend to not have any initial mark.

Please look at Making Queries and Forms


In my opinion it should be done by using many-to-many relation in Person, and form should be defined as a form class, because you don't have any validation in your form, and you are writing some html, which could be generated by putting one line of code in template. I would do it like this:

models.py

class Marksheet(models.Model):
    subname=models.CharField(max_length=50)
    marks=models.IntegerField(max_length=10)

    def __unicode__(self):
        return self.subname

class Person(models.Model):
    firstname=models.CharField(max_length=50)
    lastname=models.CharField(max_length=50)
    marksheets = models.ManyToManyField(Marksheet)

    def __unicode__(self):
        return (self.firstname,self.lastname)

forms.py from models import Person

class PersonForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(PersonForm, self).__init__(*args, **kwargs)
        # by default m2m relation is rendered with SelectMultiple widget -
        # - below line is changing it to checkbox list
        self.fields['marksheets'].widget = forms.CheckboxSelectMultiple()
    class Meta:
        model = Person

views.py

#inside your view function
if request.method == 'POST':
    form = PersonForm(request.POST)
    if form.is_valid():
        form.save()

# pass form to template in render_to_response

add.html

<form method="post" action="/newdcl/{{page_name}}/add/" > {% csrf_token %}
    {{ form }}
    <input type="submit" value="save" >
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜