Django拆分了这个单词并存储在数据库表中

时间:2013-06-25 06:22:25

标签: python django django-views

我有一个带有名称键功能的模型。在此表中,我想用逗号分隔键盘并逐个存储。实施例

userinput for keyskills = "django,python,html"

in database table(employerkeyskills)

id  |  emp_id  |  job_id  |  keyskills
----|----------|----------|------------
1   |    2     |     1    |   django
2   |    2     |     1    |   python
3   |    2     |     1    |   html

models.py

class User(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    username = models.CharField(max_length=100)
    password = models.CharField(max_length=100)

class jobs(models.Model):
    emp = models.ForeignKey(User, unique=False)
    title = models.CharField(max_length=100)
    referencecode = models.CharField(max_length=100)
    jobsummary = models.TextField()
    jobdetails = models.TextField()

class employerkeyskills(models.Model):
    emp=models.ForeignKey(User,unique=False)
    job=models.ForeignKey(jobs,unique=False)
    keyskills=models.CharField(max_length=50)

这里我想用逗号(,)分割键盘,它来自用户输入并将其一个一个地存储在EMPLOYERKEYSKILLS表中

1 个答案:

答案 0 :(得分:3)

for skill in keyskills.split(','):
  employerkeyskills.objects.create(emp=..., job=..., keyskills=skill)