1
votes

I am having 2 model of similar schema and I want to bulk copy from model1 to model2 but model2 is having 3 more fields but I want to store null in these fields.

class SubscriberBalanceProcess(models.Model):


    VOICE_SOC = models.CharField(max_length=50, null=True)
    SMS_SOC = models.CharField(max_length=50, null=True)
    DATA_SOC = models.CharField(max_length=50, null=True)
    DATE_TIME = models.DateTimeField(auto_now_add=True, blank=True)
    TOTAL_REMAIN_VOICE = models.BigIntegerField(default=0, null=True, blank=True)
    TOTAL_REMAIN_SMS = models.BigIntegerField(default=0, null=True, blank=True)
    TOTAL_REMAIN_DATA = models.BigIntegerField(max_length=100, null=True, blank=True)


    class Meta:
        db_table = "SUBSCRIBER_BALANCE_PROCESS_TEST"

class SubscriberBalance(models.Model):


    VOICE_SOC = models.CharField(max_length=50, null=True)
    SMS_SOC = models.CharField(max_length=50, null=True)
    DATA_SOC = models.CharField(max_length=50, null=True)
    DATE_TIME = models.DateTimeField(auto_now_add=True, blank=True)
    FILE_ID = models.CharField(max_length=255, null=True)


    class Meta:
        db_table = 'subscriber_balance'




SubscriberBalanceProcess.objects.bulk_create(SubscriberBalance.objects.filter(VOICE_STATUS='N', SMS_FLAG=1, TENANT_ID__in=loginIdList))

Traceback (most recent call last): File "manage.py", line 10, in execute_from_command_line(sys.argv) File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 353, in execute_from_command_line utility.execute() File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 345, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python27\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv self.execute(*args, **cmd_options) File "C:\Python27\lib\site-packages\django\core\management\base.py", line 399, in execute output = self.handle(*args, **options) File "C:\Users\Development\Desktop\python\crons\management\commands\sendsms_voicebalance_process_rule_tarif.py", line 74, in handle self.sendsmsVoicebalance() File "C:\Users\Development\Desktop\python\crons\management\commands\sendsms_voicebalance_process_rule_tarif.py", line 30, in sendsms Voicebalance activeMVNO = Functions.updateNonRuleMatchRecordByTarif(categoryId, type) File "C:\Users\Development\Desktop\python\crons\includes\functions.py", line 49, in updateNonRuleMatchRecordByTarif Functions.truncateUpdateVoice(loginIdList) File "C:\Users\Development\Desktop\python\crons\includes\functions.py", line 64, in truncateUpdateVoice SubscriberBalanceProcess.objects.bulk_create(SubscriberBalance.objects.filter(VOICE_STATUS='N', SMS_FLAG=1, TENANT_ID__in=loginIdL ist)) File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 122, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Python27\lib\site-packages\django\db\models\query.py", line 447, in bulk_create self._batched_insert(objs_with_pk, fields, batch_size) File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1056, in _batched_insert using=self.db) File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 122, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1039, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1059, in execute_sql for sql, params in self.as_sql(): File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 1019, in as_sql for obj in self.query.objs File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 968, in pre_save_val return field.pre_save(obj, add=True) File "C:\Python27\lib\site-packages\django\db\models\fields__init__.py", line 702, in pre_save return getattr(model_instance, self.attname)

AttributeError: 'SubscriberBalance' object has no attribute 'TOTAL_REMAIN_VOICE'

1
Please try to mend your question after reading this.Tobi Nary
Please read the following on how to format code (and stack traces): meta.stackoverflow.com/questions/251361/…Penguin Brian

1 Answers

4
votes

The problem is that you can't just feed SubscriberBalance objects to the SubscriberBalanceProcess creation function. You should make proper instances first with data of the common fields:

queryset = (SubscriberBalance.objects
            .filter(VOICE_STATUS='N', SMS_FLAG=1, TENANT_ID__in=loginIdList)
            .values('VOICE_SOC', 'SMS_SOC', 'DATA_SOC', 'DATE_TIME', 'FILE_ID'))

new_objects = [SubscriberBalanceProcess(**values) for values in queryset]

SubscriberBalanceProcess.objects.bulk_create(new_objects)