2017-12-05 22 views
0

データベースへの書き込み時に、以下のforループで問題が発生します。 report_name_sc = reportlistがデータベースに設定されたクエリを書き込んでいるため、最初のレコードだけが届いています。どのようにして各report_idに1行を書き込むことができますか?複数の行をデータベースに書き込むループの問題の場合

checkedlist = request.GET.getlist('report_id') 
reportlist = QvReportList.objects.filter(report_id__in= checkedlist, active = 1).values_list('report_name_sc',flat = True) 

for i in checkedlist: 
    requestsave = QVFormAccessRequest(ntname_id = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title 
            ,report_id = i, report_name_sc = reportlist, accesslevel_id = '7', phi = '1', access_beg_date = '2017-01-01', access_end_date = '2017-01-31') 
    requestsave.save() 

要求としてModels.pyが追加され、リストはREPORT_IDを取得し、チェックボックスのコレクションです確認:

from __future__ import unicode_literals 
    from django.utils import timezone 
    from django.contrib.auth.models import (AbstractBaseUser,PermissionsMixin) 
    from django.db import models 
    from django.conf import settings 
    from django.forms import ModelForm 


    class User(AbstractBaseUser, PermissionsMixin): 



     email = models.EmailField(unique=True) 
     username = models.CharField(max_length=7, unique=True) 
     formattedusername = models.CharField(max_length=11, unique=True, primary_key = True) 
     first_name = models.CharField(max_length=40) 
     last_name = models.CharField(max_length=140) 
     date_joined = models.DateTimeField(default=timezone.now) 
     is_active = models.BooleanField(default=True) 
     is_staff = models.BooleanField(default=False) 
     is_cfo = models.BooleanField(default=False) 
     facility = models.CharField(max_length=140) 
     officename = models.CharField(max_length=100) 
     jobdescription = models.CharField(max_length=140) 
     positioncode = models.CharField(max_length = 100) 
     positiondescription = models.CharField(max_length=140) 
     coid = models.CharField(max_length=5) 
     streetaddress = models.CharField(max_length=140) 
     title = models.CharField(max_length=100) 



     USERNAME_FIELD = 'username' 

     class Meta: 
      app_label = 'accounts' 
      db_table = "user" 

     def save(self, *args, **kwargs): 
      self.formattedusername = '{domain}\{username}'.format(
       domain='HCA', username=self.username) 
      super(User, self).save(*args, **kwargs); 

     def get_short_name(self): 
      return self.username 




    # REQUIRED_FIELDS = "username" 

     def __str__(self): 
      return '%s - %s %s' % (self.username, self.first_name, self.last_name) 




    class FacilityDimension(models.Model): 
     unit_num = models.CharField(db_column='Unit_Num', max_length=5, blank=True, null=True) # Field name made lowercase. 
     company_code = models.CharField(db_column='Company_Code', max_length=1, blank=True, null=True) # Field name made lowercase. 
     coid = models.CharField(db_column='Coid',primary_key=True, serialize=False, max_length=5) # Field name made lowercase. 
     coid_name = models.CharField(db_column='COID_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     c_level = models.CharField(db_column='C_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     company_name = models.CharField(db_column='Company_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     s_level = models.CharField(db_column='S_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     sector_name = models.CharField(db_column='Sector_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     b_level = models.CharField(db_column='B_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     group_name = models.CharField(db_column='Group_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     r_level = models.CharField(db_column='R_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     division_name = models.CharField(db_column='Division_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     d_level = models.CharField(db_column='D_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     market_name = models.CharField(db_column='Market_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     f_level = models.CharField(db_column='F_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     cons_facility_name = models.CharField(db_column='Cons_Facility_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     lob_code = models.CharField(db_column='LOB_Code', max_length=3, blank=True, null=True) # Field name made lowercase. 
     lob_name = models.CharField(db_column='LOB_Name', max_length=20, blank=True, null=True) # Field name made lowercase. 
     sub_lob_code = models.CharField(db_column='Sub_LOB_Code', max_length=3, blank=True, null=True) # Field name made lowercase. 
     sub_lob_name = models.CharField(db_column='Sub_LOB_Name', max_length=20, blank=True, null=True) # Field name made lowercase. 
     state_code = models.CharField(db_column='State_Code', max_length=2, blank=True, null=True) # Field name made lowercase. 
     pas_id_current = models.CharField(db_column='PAS_ID_Current', max_length=8, blank=True, null=True) # Field name made lowercase. 
     pas_current_name = models.CharField(db_column='PAS_Current_Name', max_length=40, blank=True, null=True) # Field name made lowercase. 
     pas_id_future = models.CharField(db_column='PAS_ID_Future', max_length=8, blank=True, null=True) # Field name made lowercase. 
     pas_future_name = models.CharField(db_column='PAS_Future_Name', max_length=40, blank=True, null=True) # Field name made lowercase. 
     summary_7_member_ind = models.CharField(db_column='Summary_7_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_8_member_ind = models.CharField(db_column='Summary_8_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_phys_svc_member_ind = models.CharField(db_column='Summary_Phys_Svc_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_asd_member_ind = models.CharField(db_column='Summary_ASD_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_imaging_member_ind = models.CharField(db_column='Summary_Imaging_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_oncology_member_ind = models.CharField(db_column='Summary_Oncology_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_cath_lab_member_ind = models.CharField(db_column='Summary_Cath_Lab_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_intl_member_ind = models.CharField(db_column='Summary_Intl_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     summary_other_member_ind = models.CharField(db_column='Summary_Other_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     pas_coid = models.CharField(db_column='PAS_COID', max_length=5, blank=True, null=True) # Field name made lowercase. 
     pas_status = models.CharField(db_column='PAS_Status', max_length=1, blank=True, null=True) # Field name made lowercase. 
     company_code_operations = models.CharField(db_column='Company_Code_Operations', max_length=3, blank=True, null=True) # Field name made lowercase. 
     osg_pas_ind = models.CharField(db_column='OSG_PAS_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     abs_facility_member_ind = models.CharField(db_column='ABS_Facility_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     abl_facility_member_ind = models.CharField(db_column='ABL_Facility_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     intl_pmis_member_ind = models.CharField(db_column='INTL_PMIS_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     hsc_member_ind = models.CharField(db_column='HSC_Member_Ind', max_length=1, blank=True, null=True) # Field name made lowercase. 
     loaddate = models.DateTimeField(db_column='LoadDate', blank=True, null=True) # Field name made lowercase. 

     class Meta: 
      managed = False 
      db_table = 'Facility_Dimension' 

    class QvDatareducecfo(models.Model): 
     cfo_fname = models.CharField(db_column='CFO_FName', max_length=100, blank=True, null=True) # Field name made lowercase. 
     cfo_lname = models.CharField(db_column='CFO_LName', max_length=100, blank=True, null=True) # Field name made lowercase. 
     cfo_ntname = models.OneToOneField(settings.AUTH_USER_MODEL,db_column='CFO_NTName',max_length=11, primary_key=True) # Field name made lowercase. 
     cfo_type = models.IntegerField(db_column='CFO_Type', blank=True, null=True) # Field name made lowercase. 
     org_level_id = models.IntegerField(db_column='Org_Level_ID', blank=True, null=True) # Field name made lowercase. 
     org_level = models.CharField(db_column='Org_Level', max_length=255, blank=True, null=True) # Field name made lowercase. 
     unit_no = models.CharField(db_column='Unit_No', max_length=10, blank=True, null=True) # Field name made lowercase. 
     dr_code = models.CharField(db_column='DR_Code', max_length=255, blank=True, null=True) # Field name made lowercase. 
     dr_name = models.CharField(db_column='DR_Name', max_length=255, blank=True, null=True) # Field name made lowercase. 
     cfo_dr_code = models.CharField(db_column='CFO_DR_Code', max_length=255, blank=True, null=True) # Field name made lowercase. 
     cfo_dr_name = models.CharField(db_column='CFO_DR_Name', max_length=255, blank=True, null=True) # Field name made lowercase. 
     b_level = models.CharField(db_column='B_Level', max_length=6, blank=True, null=True) # Field name made lowercase. 
     group_name = models.CharField(db_column='Group_Name', max_length=255, blank=True, null=True) # Field name made lowercase. 
     load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True) # Field name made lowercase. 
     beg_date = models.DateTimeField(db_column='Beg_Date', blank=True, null=True) # Field name made lowercase. 
     end_date = models.DateTimeField(db_column='End_Date', blank=True, null=True) # Field name made lowercase. 
     active = models.IntegerField(db_column='Active', blank=True, null=True) # Field name made lowercase. 

     class Meta: 
      managed = False 
      db_table = 'QV_DataReduceCFO' 

     def __str__(self): 
      return str(self.cfo_ntname) 





    class QvReportList(models.Model): 
     qv_dept_id = models.CharField(db_column='QV_Dept_ID', max_length=100) # Field name made lowercase. 
     report_id = models.CharField(db_column='Report_ID',primary_key=True, max_length=100, serialize=False) # Field name made lowercase. 
     report_name = models.CharField(db_column='Report_Name', max_length=255, blank=True, null=True) # Field name made lowercase. 
     report_name_sc = models.CharField(db_column='Report_Name_SC', max_length=255, blank=True, null=True) # Field name made lowercase. 
     qv_filename = models.CharField(db_column='QV_FileName', max_length=255, blank=True, null=True) # Field name made lowercase. 
     report_access = models.CharField(db_column='Report_Access', max_length=20, blank=True, null=True) # Field name made lowercase. 
     report_group_id = models.IntegerField(db_column='Report_Group_ID', blank=True, null=True) # Field name made lowercase. 
     report_sub_group_id = models.IntegerField(db_column='Report_Sub_Group_ID', blank=True, null=True) # Field name made lowercase. 
     load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True) # Field name made lowercase. 
     approver_fname = models.CharField(db_column='Approver_FName', max_length=255, blank=True, null=True) # Field name made lowercase. 
     approver_lname = models.CharField(db_column='Approver_LName', max_length=255, blank=True, null=True) # Field name made lowercase. 
     approver_ntname = models.CharField(db_column='Approver_NTName', max_length=255, blank=True, null=True) # Field name made lowercase. 
     beg_date = models.DateTimeField(db_column='Beg_Date', blank=True, null=True) # Field name made lowercase. 
     end_date = models.DateTimeField(db_column='End_Date', blank=True, null=True) # Field name made lowercase. 
     active = models.IntegerField(db_column='Active', blank=True, null=True) # Field name made lowercase. 
     approval_id = models.IntegerField(db_column='Approval_ID', blank=True, null=True) # Field name made lowercase. 
     role_based_id = models.IntegerField(db_column='Role_Based_ID', blank=True, null=True) # Field name made lowercase. 

     class Meta: 
      managed = False 
      db_table = 'QV_Report_List' 


    class QVReportAccess(models.Model): 
     user_status = models.CharField(db_column='User_Status', max_length = 20) # Field name made lowercase. 
     ntname = models.OneToOneField(settings.AUTH_USER_MODEL,db_column='NTName', max_length=11,primary_key=True, serialize=False) # Field name made lowercase. 
     report_name = models.CharField(db_column='Report_Name', max_length=50, blank=True, null=True) # Field name made lowercase. 
     report_name_sc = models.CharField(db_column='Report_Name_SC', max_length=30, blank=True, null=True) # Field name made lowercase. 
     datareduce_report_code = models.IntegerField(db_column='DataReduce_Report_Code', blank=True, null=True) # Field name made lowercase. 
     role_based_id = models.IntegerField(db_column='Role_Based_ID', blank=True, null=True) # Field name made lowercase. 
     report_id = models.OneToOneField(QvReportList,db_column='Report_ID', max_length=100, blank=True, null=True) # Field name made lowercase. 
     report_group_id = models.IntegerField(db_column='Report_Group_ID', blank=True, null=True) # Field name made lowercase. 
     report_access = models.CharField(db_column='Report_Access', max_length=50, blank=True, null=True) # Field name made lowercase. 
     sr_datareduce_summary_code = models.CharField(db_column='SR_DataReduce_Summary_Code', max_length=10, blank=True, null=True) # Field name made lowercase. 
     sr_datareduce_patient_code = models.CharField(db_column='SR_DataReduce_Patient_Code', max_length=10, blank=True, null=True) # Field name made lowercase. 
     userid = models.IntegerField(db_column='UserID', blank=True, null=True) # Field name made lowercase. 
     user_group_id = models.IntegerField(db_column='User_Group_ID', blank=True, null=True) 
     access_level_id = models.IntegerField(db_column='Access_Level_ID', blank=True, null=True) 
     active = models.IntegerField(db_column='Active', blank=True, null=True) 
     qv_statusid = models.IntegerField(db_column='QV_StatusID', blank=True, null=True) 
     employee_status_id = models.IntegerField(db_column='Employee_Status_ID', blank = True, null = True) 
     new_user = models.IntegerField(db_column='New_User', blank = True, null = True) 
     new_access = models.IntegerField(db_column='New_Access', blank = True, null = True) 
     new_report = models.IntegerField(db_column='New_Report', blank = True, null = True) 
     changed_row = models.IntegerField(db_column='Changed_Row',blank = True, null = True) 
     last_change_date = models.DateTimeField(db_column='Last_Change_Date', blank=True, null=True) # Field name made lowercase. 
     access_beg_date = models.DateTimeField(db_column='Access_Beg_Date', blank=True, null=True) # Field name made lowercase. 
     access_end_date = models.DateTimeField(db_column='Access_End_Date', blank=True, null=True) # Field name made lowercase. 
     report_beg_date = models.DateTimeField(db_column='Report_Beg_Date', blank=True, null=True) # Field name made lowercase. 
     report_end_date = models.DateTimeField(db_column='Report_End_Date', blank=True, null=True) # Field name made lowercase. 
     qv_startdate = models.DateTimeField(db_column='QV_StartDate', blank=True, null=True) # Field name made lowercase. 
     load_date = models.DateTimeField(db_column='Load_Date', blank=True, null=True) # Field name made lowercase. 

     class Meta: 
      managed = False 
      db_table = 'QV_ReportAccess' 

class QVFormAccessRequest(models.Model): 
    ntname = models.CharField(max_length=11) 
    first_name = models.CharField(max_length=40) 
    last_name = models.CharField(max_length=140) 
    coid = models.CharField(max_length=5) 
    facility = models.CharField(max_length=140) 
    title = models.CharField(max_length=100) 
    report_id = models.CharField(max_length=100) 
    report_name_sc = models.CharField(max_length=100, blank=True, null=True) 
    accesslevel_id = models.CharField(max_length=100) 
    phi = models.BooleanField(default=False) 
    access_beg_date = models.DateTimeField(blank=True, null=True) # Field name made lowercase. 
    access_end_date = models.DateTimeField(blank=True, null=True) # Field name made lowercase. 

    class Meta: 
     managed = True 
     db_table = 'QV_FormAccessRequest' 






    class DjangoMigrations(models.Model): 
     app = models.CharField(max_length=255) 
     name = models.CharField(max_length=255) 
     applied = models.DateTimeField() 

     class Meta: 
      managed = False 
      db_table = 'django_migrations' 

    class Sysdiagrams(models.Model): 
     name = models.CharField(max_length=128) 
     principal_id = models.IntegerField() 
     diagram_id = models.AutoField(primary_key=True) 
     version = models.IntegerField(blank=True, null=True) 
     definition = models.BinaryField(blank=True, null=True) 

     class Meta: 
      managed = False 
      db_table = 'sysdiagrams' 
      unique_together = (('principal_id', 'name'),) 
+0

'reportlist'はあなたの例では' QuerySet'です。これを 'QVFormAccessRequest'モデルのフィールドに割り当てることはできません。あなたのモデルを貼り付けることはできますか? –

+0

クエリーセットをチェックリストからreport_name_scを返すものに変換するにはどうすればよいですか? forループを使用してテンプレート内のレポートリストを表示しますが、forループでは保存されません。私は私の学校のネットワークに乗ることができたら明日私のモデルを貼り付けます。 – student101

+0

が追加されました。 – student101

答えて

1

あなたQVFormAccessRequestモデルは、主キーであることをntnameを指定します。したがって、同じ主キーを持つインスタンスをモデル化することはできません。

ループでは、checkedlistを反復処理していますが、常に前のオブジェクトを上書きするように同じntname_idを設定します。

達成したいことをサポートするためにモデルを変更する必要があると思われます。多分、プライマリキーの制約をntnameから削除し、Djangoが代用キー(人工キー)を作成するようにしてください。

+0

主キー制約を削除するためにモデルを更新しましたが、report_name_scは各行のクエリセット全体を公開しています。 – student101

+0

@ student101:「各行に設定されたクエリ全体をまだ公開していますか?この質問の目的のために、あなたのモデルを可能な限り減らして、問題がまだ再現可能であれば、おそらくもっと簡単でしょう。また、実際の結果と期待される結果の例を挙げると役に立ちます。 –

+0

複数の値を選択すると、report_name_sc以外の値はデータベースに正しく書き込まれます。 3つのreport_idを選択すると、各report_idの下にあるデータベースの各行のqueryset [reportname1、reportname2、reportname3]として書き出します。 – student101

関連する問題