gpt4 book ai didi

django - 在 django 中将数据从一个模型批量复制到另一个模型

转载 作者:行者123 更新时间:2023-12-02 21:08:40 30 4
gpt4 key购买 nike

我有 2 个具有相似架构的模型,我想从 model1 批量复制到 model2,但 model2 还有 3 个字段,但是我想在这些字段中存储空值。

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'

最佳答案

问题是您不能只将 SubscriberBalance 对象提供给 SubscriberBalanceProcess 创建函数。您应该首先使用公共(public)字段的数据制作适当的实例:

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)

关于django - 在 django 中将数据从一个模型批量复制到另一个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34739976/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com