Foriegnkey问题提交表单 [英] Foriegnkey issue while submitting the form

查看:117
本文介绍了Foriegnkey问题提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章在以django形式提交数据时扩展了错误/ a>

This post extends the error while submitting data in the form django

model.py

from django.db import models

# Create your models here.

class Profile(models.Model):
     name = models.CharField(max_length=50, primary_key=True)
     assign = models.CharField(max_length=50)
     doj = models.DateField()

     class Meta:
        db_table= 'profile'


     def __unicode__(self):

       return  u'%s' % (self.name)



class working(models.Model):
   w_name =models.ForeignKey(Profile, db_column='w_name')
   monday =  models.IntegerField(null=True, db_column='monday', blank=True)
   tuesday =  models.IntegerField(null=True, db_column='tuesday', blank=True)
   wednesday =  models.IntegerField(null=True, db_column='wednesday', blank=True)

   class Meta:
        db_table = 'working'



   def __unicode__(self):
      return  u'%s ' % ( self.w_name)


view.py

# Create your views here.
from forms import *
from django import http
from django.shortcuts import render_to_response, get_object_or_404


def index(request):
   obj=working()
   obj.w_name='X'
   obj.Monday=1
   obj.Tuesday=2
   obj.Wednesday =3
   obj.save()
   return http.HttpResponse('Added')

这里我想插入数据直接进入表格,如果用户点击 http://127.0.0.1:8000/

Here i want to insert the data directly into table ,if person click on http://127.0.0.1:8000/

但它会抛出以下错误任何想法?

But it throws below error any thoughts ??

异常类型:ValueError at /
异常值:无法分配u'x:working.w_name必须是配置文件实例。

Exception Type: ValueError at / Exception Value: Cannot assign "u'x'": "working.w_name" must be a "Profile" instance.

推荐答案

我以为你在说你想把值注入表单?

I thought you were saying you wanted to inject values into a form?

在这种情况下,这很清楚(看到它比评论更好),您需要将配置文件实例传递给您的工作对象,就像我们在表单中

In this case, it's clear (see it's better than comments), you need to pass in a Profile instance to your working object just as we did in the form clean method in your other post.

def index(request):
   try: 
       profile = Profile.objects.get(pk='X')
   except Profile.DoesNotExist:
       assert False # or whatever you wish
   obj=working()
   obj.w_name= profile
   obj.Monday=1
   obj.Tuesday=2
   obj.Wednesday =3
   obj.save()
   return http.HttpResponse('Added')

这篇关于Foriegnkey问题提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆