使用Django获取字段值

时间:2013-04-08 11:55:03

标签: django django-models django-views

我可以获得表格中没有显示的字段值吗?我想在会话中传递ref_id。 这是我的模特:

  def _createId():
        """
        """
        return hexlify(os.urandom(4))

class jobpost(models.Model):
    item_types = (
        ('Full Time','Full Time'),
        ('Part Time','Part Time'),
        ('Contract','Contract'),
    )
    posttype= (
        ('Job','Job'),
        ('Classified','Classified'),
        ('Project/Task','Project/Task'),
        ('Internship','Internship'),
    )
    #user = models.ForeignKey(User)
    job_id =             models.AutoField(primary_key=True)   
    country= models.ForeignKey(Country,to_field = 'country_name',  null=True)
    #user = models.ForeignKey(User, editable = False)
    post_type =        models.CharField(max_length=255,null=True, choices=posttype,default='Job')
    job_type =        models.CharField(max_length=255,null=True, choices=item_types,default='Full Time')
    job_location =    models.CharField(max_length=255,null=True)
    job_title =             models.CharField(max_length=255,null=True)
    job_description =       models.TextField(null=True)
    start_date =    models.DateField(null=True, help_text="mm/dd/yyyy")
    end_date =      models.DateField(null=True, help_text="mm/dd/yyyy")
    how_to_apply =    models.CharField(max_length=255,null=True)
    ref_id = models.CharField(max_length=32, default=_createId)

    def __unicode__(self):
        return unicode(self.country)
        return self.post_type
        return self.job_location
        return self.job_type
        return self.job_title
        return self.job_description
        return self.start_date 
        return self.end_date
        return self.how_to_apply
        return self.ref_id

表示我没有在我的表单中显示它,我想在下一个表单的会话中传递此值。 谁能告诉我怎么能这样做?以及如何以下一种形式传递表单的主键?

1 个答案:

答案 0 :(得分:1)

forms.py

class JobPostForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(JobPostForm, self).__init__(*args, **kwargs)
        self.fields['ref_id'].widget = forms.HiddenInput()

    class Meta:
        model = jobpost

views.py

def your_view(request):
    if request.method == 'POST':
        form = JobPostForm(request.POST)
        if form.is_valid():
            request.session['ref_id'] = form.cleaned_data.get('ref_id')
            pk = form.save()
            request.session['pk'] = pk.id
    else:
        form = JobPostForm()
    return render(request, page.html,{'form': form})